为了使用Word,我在Normal.dot中存储了许多公共子类和函数。我可以在任何其他Word项目中编写的任何模块中使用它们。例如,存储在Normal.dot中的这个基本函数,
Public Function BooleanString(b As Boolean) As String
If b Then BooleanString = "Yes!" Else BooleanString = "No!"
End Function可以从其他项目中使用,如
Sub TestNormal()
Debug.Print BooleanString(True)
End Sub使用Excel不能复制这种行为:我存储在Personal.xlsb中的公共子类和函数(其中之一)在从VBAProject中属于Personal.xlsb之外的任何其他电子表格的模块调用时似乎不可见,从而导致“未定义子或函数”。
我确保选中Excel选项框“信任VBA项目模型”(从丹麦语版本翻译)。此外,使用“查看”,我确保Personal.xlsb不是隐藏的,它每次打开Excel时都会显示出来。
我在这里做错了什么或者错过了什么?
向霍尔格·尼尔森问好
发布于 2020-06-28 12:00:37
在Excel中,使用Personal.xlsb的函数/Subs有点不同。
为了使用UDF函数,让我们说,testMultiplication
Function testMultiplication(x As Long, y As Long) As Long
testMultiplication = x * y
End Function它可以通过这种方式从(其他)工作表单元格中调用:
=Personal.xlsb!testMultiplication(3, 4)为了调用Personal.xlsb Subs和Functions,需要引用隐藏的工作簿VBAProject .
要做到这一点,请遵循下面的步骤:
( a)在VBE - Project中,右键单击其VBAProject,选择VBAProject Properties...并更改标准名称,让我们说,'PersVBAProject. b) Then click on a module of the workbook you want to add the reference and use Tools -> References. Find 'PersVBAProject,检查它和
从那一刻起,
直接调用Prsonal.xlsb的子类/函数。
Sub AddPersonalXLSBRef()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility ...'
Dim VBe As VBIDE.VBe, vbPr As VBIDE.VBProject
Dim objRef As VBIDE.Reference, boolFound As Boolean
Set VBe = Application.VBe
Set vbPr = ActiveWorkbook.VBProject
'check if reference to Personal.xlsb exists:
'Please, take care to have its VBAProject name modified in `PersVBProject`
'or something else and, in such a case, adapt the following lines accordingly
For Each objRef In vbPr.References
If objRef.Name = "PersVBProject" Then
MsgBox "Reference to Personal.xlsb already exists"
Exit Sub
End If
Next
'add the reference, if not exist:
vbPr.References.AddFromFile Application.StartupPath & "\Personal.xlsb"
MsgBox "Reference to Personal.xlsb added Successfully"
End Sub它将引用添加到活动工作簿项目。代码可以修改为将其添加到所有打开的工作簿中(尚未添加).
https://stackoverflow.com/questions/62622038
复制相似问题