excelvba如何打开,关闭多个工作薄

用excel VBA 编写打开和关闭多个工作薄,相应的代码如下:

1、打开指定工作簿

dim wb as workbook

set wb = " 文件路径及文件名"

workbooks.open filename:= wb

2、关闭所有工作簿并保存

workbooks.close

3、打开多个工作薄的程序代码:

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

AI助手