【问题标题】:Programmatically update the code of a CodeModule. Issue with character "_" in original copied code以编程方式更新 CodeModule 的代码。原始复制代码中的字符“_”问题
【发布时间】:2021-09-07 09:49:45
【问题描述】:

我正在更新从另一个模块复制它的模块的代码。我替换了整个代码。 实际上,代码在工作表(原始和目标)中,而不是在模块中。

复制它的代码是:

Dim codeModDestination As CodeModule
Dim codeModOriginal As CodeModule
                
'..... Set CodeModules, etc.
                
'Delete the existing code at the destination CodeModule    
codeModDestination.DeleteLines 1, codeModDestination.CountOfLines 

Dim fila As Long
For fila = 1 To codeModOriginal.CountOfLines
    codeModDestination.InsertLines codeModDestination.CountOfLines + 1, _
                                   codeModOriginal.Lines(fila, 1)
Next fila

它复制了原始模块的整个代码,但是用字符“_”分割的行在它们之间有一个空行,导致代码启动编译错误:

原码:

    Msgbox "Hello" + vbLf + _
        "World"

转化为:

    Msgbox "Hello" + vbLf + _

        "World"

所有其他代码都正确复制,但我无法进入中断模式,所以我不知道发生了什么。

编辑:

我找到了一种解决方法,但我不确定它为什么有效:

Dim fila, insert_before As Long
insert_before = 0
For fila = 1 To codeModOriginal.CountOfLines
    codeModDestination.InsertLines codeModDestination.CountOfLines + 1 - insert_before, _
                                   codeModOriginal.Lines(fila, 1)
    insert_before = 0
    If fila <> codeMod.CountOfLines Then
        debug.print fila
        insert_before = 1
    End If

Next fila

insert_before 在前 20 行中为 0,直到找到带有“_”的第一条指令。那么它总是 1。

【问题讨论】:

  • 你说“我替换了整个代码。”。如果这样做,为什么不删除此模块并导入要从中复制的模块?
  • 不是模块,而是有数据的电子表格,我删不掉。我们正在做的是使用旧代码(但有用的数据)复制电子表格,然后使用该函数更新代码。
  • 我们正在更新的电子表格代码是 Worksheet_Change 子代码,不能放在其他地方
  • 在工作表模块中,您只想将Worksheet_Change替换为另一个模块中的Worksheet_Change代码,或者删除所有工作表模块代码并从另一个模块中粘贴新的Worksheet_Change模块?
  • @Eliofernandes 我们更改了Worksheet_Change sub,但也有一些常量声明,所以最好替换整个代码

标签: excel vba


【解决方案1】:

您已经拥有删除模块内容的代码。

我的解决方案是从您想要的模块中获取代码,并使用 InsertLines 而不是使用循环将其插入到目标模块中。

Main 过程,展示了如何将所有代码从 sheet1 复制到 sheet2 的示例。

Option Explicit

Public code As String

Private Sub Main()
    ' Find procedure
    code = Module_GetAllContent("Sheet1")
    Debug.Print code
    
    ' Add procedure to code module
    If code <> vbNullString Then Call Module_AddCode("Sheet2")
End Sub

'----------------------------------------------------------
' Get all module content
'----------------------------------------------------------
Function Module_GetAllContent(ByVal codName As String) As String
    On Error GoTo errProject
        With ActiveWorkbook.VBProject.VBComponents(codName).CodeModule
            code = .Lines(1, .CountOfLines)
        End With
    
    ' Return
    Module_GetAllContent = code

exitRoutine:
    ' Exit
    Exit Function

errProject:
    Debug.Print Err.Description
    Resume exitRoutine
End Function

'----------------------------------------------------------
' Add code to module
'----------------------------------------------------------
Sub Module_AddCode(ByVal codName As String)
    On Error GoTo errProject
        With ActiveWorkbook.VBProject.VBComponents(codName).CodeModule
            .InsertLines .CountOfLines + 1, code
        End With
        
exitRoutine:
    Exit Sub

errProject:
    Debug.Print Err.Description
    Resume exitRoutine
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    相关资源
    最近更新 更多