我正在尝试在 Excel 中创建一个按钮来触发用户选择文件夹位置的屏幕,并将其所有 Excel 文件添加到工作表中。
Sub Getsheets()
Path = GetFolder("N:\", "Select an Input Folder")
Filename = Dir(Path & "*.xlsm")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Application.DisplayAlerts = False
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
Function GetFolder(strPath As String, fldSt As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = fldSt
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
这是我到目前为止所拥有的,如果我不合并该getfolder
功能,我可以检索指定文件夹中的所有 Excel 文件,但是一旦我将功能放入,什么也没有发生。如何解决这个问题?