我有一个自动excel(excel A),可以从另一个excel(excel B)复制和粘贴信息。 我已经控制了excelB是否存在。
我想这样做:
If "workbookExcelBisopen" Then 'what can i put inside the if
'What can i put here for make the code work
Else
Set ExcelB = Application.Workbooks.Open(".xlsx", False, True)
End if
Sheets("Sheet1excelB").Select
我得到的主要问题是,如果工作簿已经打开,则会显示一条消息,指出“ ExcelB已经打开。您要保存所做的更改吗?”。我想避免这种消息。
谢谢你。
答案 0 :(得分:0)
感谢@Our Man in Bananas解决方案在SO中的实现。
Sub test()
Dim wb As Workbook
Set wb = GetWorkbook("C:\Users\dick\Dropbox\Excel\Hoops.xls")
If Not wb Is Nothing Then
Debug.Print wb.Name
End If
End Sub
Public Function GetWorkbook(ByVal sFullName As String) As Workbook
Dim sFile As String
Dim wbReturn As Workbook
sFile = Dir(sFullName)
On Error Resume Next
Set wbReturn = Workbooks(sFile)
If wbReturn Is Nothing Then
Set wbReturn = Workbooks.Open(sFullName)
End If
On Error GoTo 0
Set GetWorkbook = wbReturn
End Function
谢谢你的答案。