我想设置一个变量来始终引用特定的工作簿;现在我按照下面列出的方式进行,但我不希望它每次打开文档。我该怎么办呢?另请注意,如果需要更改任何变量或其他变量。
Dim wbkC as workbook
Set WbkC = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls")
答案 0 :(得分:2)
如果不是,您可以使用自定义功能打开它,如果是,则可以使用打开的副本。
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