将工作簿分配给变量

时间:2017-10-24 04:58:39

标签: excel-vba vba excel

我想将ActiveWorkbook设置为变量当工作簿同时打开ActiveWorkbook分配给变量时,我可以在整个VBA excel项目中使用该变量。 我试图在Workbook_open()函数中分配ThisWorkbook excel对象,但它不起作用。我在下面提供了该代码。

Private Sub Workbook_Open() On Error Resume Next Set WRBK = ActiveWorkbook #If Mac Then #Else 'unprotectVBProjProp UnlockVBA ' Sujith ID: 12482 AddReferences ' Sujith ID: 12482 ' protectVBProjProp #End If 'MsgBox "xla Workbook opened" Set eventInstance = New bwEvents End Sub

那么如何将activeworkbook设置为变量?

1 个答案:

答案 0 :(得分:0)

我不太确定中间的所有命令是什么,例如#If Mac ThenUnlockVBA

如果您要将ActiveWorkbook设置为对象WRBK,则需要将WRBK定义为Public,然后使用类似代码的内容下面:

ThisWorkbook 模块中的代码

Private Sub Workbook_Open()

    Set WRBK = ActiveWorkbook

    TestWorkbookName ' call sub <--  this is just to test the the workbook was assigned correctly

End Sub

常规模块中的代码

Option Explicit

Public WRBK As Workbook

Sub TestWorkbookName()

MsgBox WRBK.Name

End Sub