簡體   English   中英

為什么我在 VBA 循環中遇到 Dir 錯誤?

[英]Why am I getting an error with Dir in a VBA loop?

我一直在構建一個宏來遍歷文件夾中的一系列文件,並將每個文件復制並粘貼到另一個文件夾中的一系列其他工作表中。 我從下面的這段代碼開始,它可以很好地進行復制和粘貼:

'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls*"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
      DoEvents

    'Change First Worksheet's Background Fill Blue
      wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)

    'Save and Close Workbook
      wb.Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

但是,我現在添加了第二個循環來處理多個到多個文件,並且在第二個版本中出現運行時錯誤 5:

myFile = Dir

我已將 myFile 重命名為另一個名稱,因此它與第一個名稱不沖突。

我沒有在這里發布我所有的代碼,因為它更長更復雜。 希望這足以讓你們繼續下去嗎?

您無法使用Dir() Function實現這一點。 為此,您可以使用Scripting.FileSystemObject
這是我寫的一個示例,您可以輕松地適應您的代碼:

Dim oFile       As Object
Dim oFSO        As Object
Dim oFolder     As Object
Dim oFiles      As Object

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(myPath) 'You must initialize this before
Set oFiles = oFolder.Files

'For all files in the folder
For Each oFile In oFiles
    If (oFile Like "*.xls*") Then
        'Set variable equal to opened workbook
        Set wb = Workbooks.Open(Filename:=oFile)

        'Ensure Workbook has opened before moving on to next line of code
         DoEvents

        'Change First Worksheet's Background Fill Blue
        wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)

        'Save and Close Workbook
        wb.Close SaveChanges:=True

        'Ensure Workbook has closed before moving on to next line of code
        DoEvents
    End If
Next

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM