[英]Excel VBA - Determine if a Module is included in a project
I am currently working on a project where I am unable to verify that all modules are installed. 我目前正在一个无法验证是否已安装所有模块的项目中。 There is a growing group of modules being used for common functions for programs I work with. 越来越多的模块用于我使用的程序的通用功能。 I have tried some solutions on the web which I couldn't use as I am unfamiliar with Activeworkbook.VBProject.VBComponents()
methods. 我在网上尝试了一些无法使用的解决方案,因为我不熟悉Activeworkbook.VBProject.VBComponents()
方法。
It was mentioned that I should check tools reference for Microsoft Visual Basic For Applications Extensibility and I checked that with no result. 提到要检查Microsoft Visual Basic应用程序可扩展性的工具参考,但没有检查。 Any help would be appreciated. 任何帮助,将不胜感激。 :) :)
References: 参考文献:
https://www.mrexcel.com/forum/excel-questions/284317-vba-function-check-if-particular-macro-exists.html https://www.mrexcel.com/forum/excel-questions/284317-vba-function-check-if-particular-macro-exists.html
https://www.devhut.net/2010/12/09/ms-access-vba-determine-if-a-module-exists/ https://www.devhut.net/2010/12/09/ms-access-vba-determine-if-a-module-exists/
here is my code: 这是我的代码:
Option Explicit
Public Function Is_Module_Loaded(name As String) As Boolean
Dim Module As Object
Dim Module_Name As String
Module_Name = name
Is_Module_Loaded = False
On Error GoTo errload
Set Module = ActiveWorkbook.VBProject.VBComponents(Module_Name).CodeModule
Is_Module_Loaded = True
If (0 <> 0) Then
errload:
MsgBox ("MODULE: " & Module_Name & " is not installed please add")
Stop
End If
End Function
When Running the Code I don't get any error that is very helpful excluding my own which is reporting wrong saying my module is absent when it isn't. 当运行代码时,我没有得到任何非常有用的错误,除了我自己的错误,它报告了错误,说我的模块不在,这是错误的。
EDIT: updated to add the workbook as a second parameter 编辑:更新以将工作簿添加为第二个参数
Try this: 尝试这个:
Sub tester()
Debug.Print Is_Module_Loaded(ThisWorkbook, "Module4")
Debug.Print Is_Module_Loaded(ActiveWorkbook, "Module4")
End sub
Public Function Is_Module_Loaded(wb as Workbook, name As String) As Boolean
Dim Module As Object
On Error Resume Next
Set Module = wb.VBProject.VBComponents(name).CodeModule
On Error GoTo 0
Is_Module_Loaded = Not Module Is Nothing
If Not Is_Module_Loaded Then
MsgBox ("MODULE: " & name & " is not installed in '" & _
wb.Name & "' please add")
End If
End Function
So I believe I have found a solution. 所以我相信我已经找到了解决方案。
Credit to : Tim Williams, Mathieu Guindon and Joe Phi (See link) for guidance to the solution 感谢 :蒂姆·威廉姆斯,马修金敦和乔岛(见链接)指导解决方案
Reference : ( https://stackoverflow.com/a/46727898/10297459 ) 参考 :( https://stackoverflow.com/a/46727898/10297459 )
Noted problems: With the original Tim mentioned that not setting a workbook could have me referencing the proper workbook, this was the major problem as I had other workbooks open that it was trying to reference. 注意到的问题:最初的Tim提到没有设置工作簿可能会使我引用正确的工作簿,这是主要问题,因为我打开了其他正在尝试引用的工作簿。
Option Explicit
Public Function Is_Module_Loaded(name As String, Optional wb As Workbook) As Boolean
'!!!need to reference: microsoft visual basic for applications extensibility 5.3
Dim j As Long
Dim vbcomp As VBComponent
Dim modules As Collection
Set modules = New Collection
Is_Module_Loaded = False
'check if value is set
If wb Is Nothing Then
Set wb = ThisWorkbook
End If
If (name = "") Then
GoTo errorname
End If
'collect names of files
For Each vbcomp In ThisWorkbook.VBProject.VBComponents
If ((vbcomp.Type = vbext_ct_StdModule) Or (vbcomp.Type = vbext_ct_ClassModule)) Then
modules.Add vbcomp.name
End If
Next vbcomp
'Compair the file your looking for to the collection
For j = 1 To modules.Count
If (name = modules.Item(j)) Then
Is_Module_Loaded = True
End If
Next j
j = 0
'if Is_module_loaded not true
If (Is_Module_Loaded = False) Then
GoTo notfound
End If
'if error
If (0 <> 0) Then
errorname:
MsgBox ("Function BootStrap.Is_Module_Loaded Was not passed a Name of Module")
Stop
End If
If (0 <> 0) Then
notfound:
MsgBox ("MODULE: " & name & " is not installed please add")
Stop
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.