我想合并特定文件夹中的所有Excel文件。我为要合并的文件所在的文件夹路径创建了一个输入框。然后我有了文件名的公式,但这个公式不起作用。它给出了值Filename=""。为什么会发生这种情况?如何修复呢?
Dim Path as String
Dim Filename as String
Path = InputBox("Paste the path of the folder with files to consolidate")
Filename = Dir(Path & "*.xls*", vbNormal)发布于 2017-10-12 18:19:51
为什么不使用Excel自己的文件夹选择器呢?试试这段代码。
Function PickedFolder() As String
Dim Dlg As FileDialog
Dim Ffn As String
Ffn = Application.DefaultFilePath & "\"
Set Dlg = Application.FileDialog(FileDialogType:=msoFileDialogFolderPicker)
With Dlg
.Title = "Select the folder to consolidate"
.InitialView = msoFileDialogViewList
.InitialFileName = Ffn
.AllowMultiSelect = False
If .Show = True Then PickedFolder = .SelectedItems(1)
End With
End Function此函数返回用户选择的路径。您可以在文本框中输入它,也可以直接合并在其中找到的文件。
https://stackoverflow.com/questions/46705908
复制相似问题