2017-10-12 93 views
0

我想整合特定文件夹中的所有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) 
+0

什么'Path'评估?你确定它找到正确的位置 – Tom

+1

并且那些文件存在于这些类型的路径吗? – QHarr

+0

在循环之前,您是否指定了带有Filename = Dir的文件名? – QHarr

回答

1

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

该函数返回用户选择的路径。您可以将其输入到您的文本框中,或直接进行整合其中找到的文件。