优化 msoFileDialogOpen

Optimize msoFileDialogOpen

我试图让这个 msoFileDialogOpen 允许用户 select 多个文件。有没有更好的方法:

Public Sub Function3_FileExplorer()
    ' Start File Explorer to select file containing data (simple GUI, much
    ' easier than coding vFileName)
    vuserChoiceDataFileNumber = InputBox("Enter the number of files you want to select.")
    With Application.FileDialog(msoFileDialogOpen)
        Select Case IsNumeric(vuserChoiceDataFileNumber)
            Case True
                If VarType(vuserChoiceDataFileNumber) = 2 Or 3 Then
                    iuserChoiceDataFileNumber = CInt(vuserChoiceDataFileNumber)
                End If
            Case False
                MsgBox (vuserChoiceDataFileNumber & " is not an integer.")
                .AllowMultiSelect = False
        End Select
        .Show
    End With
    Exit Sub
    On Error GoTo ErrorHandler
    .AllowMultiSelect = True
    ErrorHandler:
    MsgBox "Error detected" & vbNewLine & "Error" & Err.Number & ": " & _
        Err.Description, vbCritical, "Error Handler: Error " & Err.Number
    MsgBox "If you want to force the program to run, go to the line below and " & _
        "insert a ' mark to comment the line out." & vbNewLine & _
        "On Error GoTo ErrorHandler", vbCritical, "Error Handler: Error " & Err.Number
End Sub

是的,您可以通过不询问用户他们想要打开多少个文件来简化这件事——让他们select想打开多少就打开多少。

Public Sub Function3_FileExplorer()
    '   Start File Explorer to select file containing data (simple GUI, much easier than coding vFileName)
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .FilterIndex = 2
        If .Show Then
            Dim file As Variant
            For Each file In .SelectedItems
                ' do something with the file, for example, open it:
                Application.Workbooks.Open (file)
            Next file
        End If
    End With
End Sub