2015-12-10 86 views
1

我想删除目录中的文件,然后目录如果用户提示的答案是否定的。在excel 2010 vba下面,但是我在For这行上得到一个语法错误。谢谢 :)。删除文件,然后在Excel 2010文件夹vba

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 

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

+0

'myfile'只是一个字符串,不是数组,所以'i'是一个字符串,不是数字。看看'dir' – findwindow

回答

2

用户需要通过文件使用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 

您可以修改此杀文件,而不是msgboxing它。

+0

非常感谢你:)。 – Chris

相关问题