下面的VBA代碼先用Application.GetOpenFilename方法顯示“打開”文件對話框,然后打開所選擇的一個或多個Excel工作簿文件。
Sub OpenWorkbooks()
On Error Resume Next
Dim SelectFiles As Variant
‘顯示打開文件對話框
SelectFiles = Application.GetOpenFilename("Excel 文件 (*.xl*)," & "*.xl*", , "打開", , True)
‘未選擇
If TypeName(SelectFiles) = "Boolean" Then
Exit Sub
End If
‘打開所選工作簿
For i = 1 To UBound(SelectFiles)
Workbooks.Open SelectFiles(i)
Next i
End Sub