我想设置一个变量以始终引用特定的工作簿;现在我正在按照下面列出的方式进行操作,但我不希望它每次都打开文档。我应该有什么?另请注意是否有任何变量需要更改或类似的内容。
Dim wbkC as workbook
Set WbkC = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls")
如果不是,您可以使用自定义函数将其打开,或者如果是,则获取打开的副本。
Public Sub Main()
Dim wbkC As Workbook
'This will open it
Set wbkC = GetWorkbookReference("G:\Reporting\ReportCompare.xls")
'This will not open it b/c it's already open
Set wbkC = GetWorkbookReference("G:\Reporting\ReportCompare.xls")
End Sub
Public Function GetWorkbookReference(ByVal sFullName As String) As Workbook
Dim wbReturn As Workbook
On Error Resume Next
'If it's open, get a reference to it
Set wbReturn = Workbooks(Dir$(sFullName))
On Error GoTo 0
'If it's not open, open it
If wbReturn Is Nothing Then
Set wbReturn = Workbooks.Open(sFullName)
End If
Set GetWorkbookReference = wbReturn
End Function