2015-12-10 113 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

相關問題