MultiSelect上的Excel VBA GetOpenFileName错误:= True

时间:2014-08-07 10:24:30

标签: excel vba excel-vba

我得到的错误类型不匹配,请帮助我新的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

1 个答案:

答案 0 :(得分:4)

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

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

见这个例子。

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