Excel/VBA 代码忽略输入的参数

Excel/VBA code ignores inputted arguments

我有一个 VBA 子程序,它应该在需要时加载到用户代码模块中。 我的问题是,当我通过 ("ModuleA",".bas") 时,代码将 return "ModuleB"。 ModuleB 在指定的文件路径中不存在(已删除)。

这段代码如何 return 一个不存在的文件,当它被专门传递一个不同的值时? 'filepath' 变量包含正确的路径,它被正确地传递给导入语句。

此外,"Remove" 语句不会删除传递给它的模块。

我从来没有 运行 遇到过这样的问题,我不知道该怎么做。

我已经尝试过:重新启动 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 中(直接)查看或编辑模块和成员属性。