合并文件

我想整合特定文件夹中的所有Excel文件。 我创build了一个input框,用于合并文件的path。 然后我有文件名公式,该公式不起作用。 它给出值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) 

为什么不使用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 

该函数返回用户select的path。 您可以将其input到文本框中,也可以直接合并在其中find的文件。