我有一个VBA子,应在需要时在用户代码模块中加载。我的问题是,当我通过(“ ModuleA”,“。bas”)时,代码将返回“ ModuleB”。指定的文件路径中不存在(删除)ModuleB。
当代码专门传递了不同的值时,该代码如何返回不存在的文件?'filepath'变量包含正确的路径,并且正在正确地传递给import语句。
此外,“删除”语句不会删除传递给它的模块。
我从来没有遇到过这样的问题,并且对如何做一无所知。
我尝试过的操作:重新启动excel / PC,重命名模块以更改路径,在子末添加代码以删除模块。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''SJH
'LoadModule
'
'Loads in a module with a specified name from the BigData Directory
'
'extension includes the ., so .frm or .bas
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub LoadModule(ByVal ModuleName As String, ByVal extension As String)
ThisWorkbook.Activate
Err.Clear
'handle errors in-line...
On Error Resume Next
'include reference to "Microsoft Visual Basic for Applications Extensibility"
Dim vbproj As VBIDE.VBProject
Dim vbc As VBIDE.VBComponent
Dim filepath As String
filepath = ("\\uslafnas01\GE_LAB\BigData\" & ModuleName & extension)
Set vbproj = ActiveWorkbook.VBProject
'Error will occur if component with this name is not in the project
Set vbc = vbproj.VBComponents(ModuleName)
If Err.Number <> 0 Then
Err.Clear
'so add it...
vbproj.VBComponents.Import filepath
If Err.Number <> 0 Then
MsgBox ("Could not import " & ModuleName & " Module: " & filepath)
End If
Else
'no error - vbc should be valid object
'remove existing version first before adding new version
vbproj.VBComponents.Remove vbc
vbproj.VBComponents.Import filepath
If Err.Number <> 0 Then
MsgBox ("New " & ModuleName & " couldn't replace old " & ModuleName & " Module " & filepath)
End If
End If
'Set vbc = Nothing
'Set vbproj = Nothing
End Sub
模块的名称不是由文件名决定的,而是由一个隐藏的VB_Name
属性决定的,例如在记事本中打开模块时,您可以看到该属性。
如果您ModuleA.bas
在记事本中打开,我怀疑您会在上面的第一行看到它Option Explicit
:
Attribute VB_Name = "ModuleB"
文件名无关紧要,此属性确定VBA模块的程序名称。
无法在VBE中直接查看或编辑模块和成员属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句