我没有在代码中提供文件路径,而是提示用户选择包含xlsx格式的数据文件的文件夹,该文件夹通过For Each wbFile In fldr.Files
循环播放。
我收到错误424。
查看代码中注释掉的行:
Sub getDataFromWbs()
Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")
'Set fldr = fso.GetFolder("\\name_of_folder_to_get_files_from\") 'old code
'Dim FolderName As String 'fldr was previously, original code: FolderName
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
fldr = .SelectedItems(1) 'fldr was previously, original code: FolderName
Err.Clear
On Error GoTo 0
End With
i = "1" 'set integer for loop over sheets
For Each wbFile In fldr.Files 'loop over data in prompted folder
答案 0 :(得分:0)
以下是您的代码的快速修复:
Sub getDataFromWbs()
Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")
'Set fldr = fso.GetFolder("\\name_of_folder_to_get_files_from\") 'old code
'Dim FolderName As String 'fldr was previously, original code: FolderName
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
FolderName = .SelectedItems(1) 'fldr was previously, original code: FolderName
Err.Clear
On Error GoTo 0
End With
i = "1" 'set integer for loop over sheets
Set fldr = fso.GetFolder(FolderName)
For Each wbFile In fldr.Files 'loop over data in prompted folder