MultiSelect 上的 Excel VBA GetOpenFileName 错误:=True

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25180061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:07:31  来源:igfitidea点击:

Excel VBA GetOpenFileName error on MultiSelect:=True

excelvbaexcel-vba

提问by John Paul

Im getting error type mismatch, please help im new to vba macro not sure what im doing. I just want the code to be able to select multiple files on search

我遇到错误类型不匹配,请帮助我不知道我在做什么 vba 宏新手。我只希望代码能够在搜索时选择多个文件

Sub Main()

On Error GoTo Error:

    'Open File to search
    myFile = Application.GetOpenFilename(MultiSelect:=True)

    bFirstLineExtract = True
    bFirstLineLog = True
    CellRowCounter = 2
    bFound = False

    'Get First Cell Value
    CellValue = Cells(CellRowCounter, 1)

    Do Until (CellValue = "") Or (CellValue = Null)
        Open myFile For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                If InStr(textline, CellValue) Then
                    sCreateExtract
                    bFound = True
                End If
            Loop
        If bFound = False Then
            sCreateLog
        End If
        Close #1

        CellRowCounter = CellRowCounter + 1
        CellValue = Cells(CellRowCounter, 1)
    Loop
    Close #1

Exit Sub

Error:
    MsgBox ("Error in Main subroutine - " & Err.Description)

End Sub

回答by Siddharth Rout

Like I mentioned in the comments above

就像我在上面的评论中提到的

You can't use myfilelike that. You have to loop through the collection

你不能那样用myfile。你必须遍历集合

See this example.

请参阅此示例。

Sub Sample()
    Dim myFile As Variant
    Dim i As Integer

    'Open File to search
    myFile = Application.GetOpenFilename(MultiSelect:=True)

    If IsArray(myFile) Then  '<~~ If user selects multiple file
        For i = LBound(myFile) To UBound(myFile)
            MsgBox myFile(i)
        Next i
    Else '<~~ If user selects single file
        MsgBox myFile
    End If
End Sub