'FileFilter String 文件类型过滤器 "所有文件 (.), ."
'FilterIndex Integer 默认使用的过滤器索引 1
'Title String 对话框标题 "打开"
'ButtonText String 按钮显示文字(仅Mac) -
'MultiSelect Boolean 是否允许多选 默认False

Sub 单选()

Dim filepath As String
Dim fileFilter As String
Dim path As String
'定义文件过滤器
fileFilter = "Excel文件 (*.xlsx;*.xls),*.xlsx;*.xls," & _
            "文本文件 (*.txt),*.txt," & _
            "所有文件 (*.*),*.*"

filepath = Application.getopenfilename(fileFilter, 3, "选择你的文件")

If filepath = "False" Then Exit Sub
path = filepath

' MsgBox filepath
End Sub

Sub 多选()
Dim selectedFiles As Variant
Dim i As Integer
Dim msg As String

' MultiSelect设为True启用多选
selectedFiles = Application.getopenfilename( _
    fileFilter:="Excel文件 (*.xlsx;*.xls), *.xlsx;*.xls", _
    MultiSelect:=True _
)

' 多选时返回的是数组
If IsArray(selectedFiles) Then
    msg = "选择了 " & UBound(selectedFiles) & " 个文件:" & vbCrLf
    For i = LBound(selectedFiles) To UBound(selectedFiles)
        msg = msg & selectedFiles(i) & vbCrLf
    Next i
    MsgBox msg
Else
    Exit Sub
End If

End Sub
多选和单选的主要区别在于,单选返回结果是false或者string;多选的返回结果是一个一维数组(下标1)或者false。

posted on 2025-11-05 23:46  青竹小轩  阅读(2)  评论(0)    收藏  举报