百科网

首页 > 科技数码

科技数码

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

科技数码佚名2024-01-03

用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)

"未选择
(www.ws46.cOm)

If TypeName(SelectFiles) = "Boolean" Then

Exit Sub

End If

"打开所选工作簿

For i = 1 To UBound(SelectFiles)

Workbooks.Open SelectFiles(i)

Next i

End Sub

打赏