VBA 如何关闭指定的多个excel文件,如果指定的多个文件中有几个并未打开,如何跳过未打开的文件。

2022-08-06 财经 105阅读
您可以参考下面的代码,看看能否帮到你
Sub closeObject()
Dim xlExcel As Object, wb1 As Workbook, wb2 As Workbook, wb3 As Workbook, wb4 As Workbook, wb5 As Workbook
Set xlExcel = CreateObject("excel.application")
Set wb1 = xlExcel.workboos("1#站每日库存表.xlsm")
Set wb2 = xlExcel.workboos("4#站每日库存表.xlsm")
Set wb3 = xlExcel.workboos("16#站每日库存表.xlsm")
Set wb4 = xlExcel.workboos("27#站每日库存表.xlsm")
Set wb5 = xlExcel.workboos("76#站每日库存表.xlsm")
If wb1 Is Nothing Then
MsgBox "1#站每日库存表 不存在", vbOKOnly, "===> Warning"
Else
wb1.Close False
End If
If wb2 Is Nothing Then
MsgBox "4#站每日库存表.x不存在", vbOKOnly, "===> Warning"
Else
wb2.Close False
End If
If wb3 Is Nothing Then
MsgBox "16#站每日库存表 不存在", vbOKOnly, "===> Warning"
Else
wb3.Close False
End If
If wb4 Is Nothing Then
MsgBox "27#站每日库存表 不存在", vbOKOnly, "===> Warning"
Else
wb4.Close False
End If
If wb5 Is Nothing Then
MsgBox "76#站每日库存表 不存在", vbOKOnly, "===> Warning"
Else
wb5.Close False
End If

End Sub
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com