如果Excel Workbook已打开,则...... VBA [英] if Excel Workbook is open then...... VBA
本文介绍了如果Excel Workbook已打开,则...... VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该怎么写代码.
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
如果 excel 工作簿已打开,则....
if an excel Workbook is already open then....
Set xlApp = GetObject(, "Excel.Application")
elseif xlApp is nothing then
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("E:\InspectionCreator\InspectionSheet.xlsx")
End if
我不希望它必须是特定的工作簿,就像我似乎无法在互联网上找到任何东西的任何工作簿一样.任何帮助都会很棒.
I don't want it to have to be a specific workbook just any workbook I can't seem to find anything on the internet. Any help would be awesome.
推荐答案
首先尝试使用getobject:如果抛出错误,则使用createobject:
First try using getobject: if it throws an error then use createobject:
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
On Error GoTo 0
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
End If
这篇关于如果Excel Workbook已打开,则...... VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文