【发布时间】:2019-01-24 15:48:36
【问题描述】:
我有一个 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
【问题讨论】:
-
尝试删除
On Error Resume Next语句,看看您遇到了哪些其他错误。仅当您预期某行代码出错并在该行之后设置回On Error Goto 0时才执行此操作。然后让我们知道发生了什么。