删除文件,然后在Excel 2010 VBA中的文件夹

Justaguy

我正在尝试删除目录中的文件,如果用户提示的答案为否,则删除该目录中的文件。在下面,excel 2010 vba但是我在For网上遇到语法错误谢谢 :)。

Dim Directory As String
Dim MyFile As Variant
Dim MyFolder As String
Dim i As Long
iYesNo = MsgBox("Do the patients and barcode match the setup sheet?", vbYesNoCancel)
      Select Case iYesNo
             Case vbYes
             GoTo Line2
             Case vbNo
             MsgBox ("Doesn't match! Please enter again")
             MyFolder = Directory ' delete all txt files in the folder
             MyFile = MyFolder & "*.txt"
             For i = LBound(MyFile) To UBound(MyFile)
               Kill MyFolder & MyFile(i)
             Next
               RmDir Directory  ' delete folder
             GoTo Line1
       End Select

该目录由一个变量设置为,当我单步执行代码时,该变量似乎是正确的。

Dan Donoghue

您需要使用DIR并遍历文件:

这将列出文件夹中的文件:

Sub MyFileStuff()
Dim MyFile As String, MyFolder As String
MyFolder = "N:\"
MyFile = Dir(MyFolder & "*.*")
Do Until MyFile = ""
    MsgBox MyFile
    MyFile = Dir
Loop
End Sub

您可以修改此选项以杀死文件,而不是将其msgbox装箱。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章