用自动化替换Application.Getopenfilename

时间:2014-11-23 00:54:34

标签: vba

我希望将下面的代码转换为不需要用户选择文件的代码。

此代码用于从特定文件夹中的所有工作簿中选择特定工作表,说“C:\ Test”

这是合并宏的一部分

sub open_issues_sheet()
Dim Files as Variant 

Files = Application.GetopenFilename("Excel FIles (*xl*),*xl*",Title:="Select Files",         Multiselect:=True)

 For Z = LBound(Files) To UBound(Files)
 tem=Split(Files(Z),"\")
 If(tem(UBound(tem)) <> ThisWorbook.Name) Then 

 Set S= Workbooks.Open(Files(Z))

 S.Sheets("Issues").Select

'要复制到当前工作表的代码

我尝试使用此http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/

但我在“For Z = LBound”

行遇到“类型不匹配”错误

1 个答案:

答案 0 :(得分:1)

假设您仍然需要用户选择包含需要合并的文件的文件夹,使用FileDialog(msoFileDialogFolderPicker)将是可接受的解决方案。

Dim sFilePath As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Title = "Select folder to consolidate"
    If .Show = -1 Then

        'Get the first file listed in the folder
        sFilePath = Dir(.SelectedItems(1) & "\")

        Do While Not sFilePath Like vbNullString

            'Verify the extension before opening file
            If Mid$(sFilePath, InStrRev(sFilePath, ".")) Like ".xls" Then

                ' Perform task ...

            End If

            'Get next file
            sFilePath = Dir
        Loop
    End If
End With