2

我想从另一个代码 VBA 中评论包含 Msg Box 的行。我正在尝试使用 Library VBA EXTENSIBILITY,但我没有找到解决方案。

欢迎任何帮助。

这是我的代码:

Sub CommentCode()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim LineNum As Long
        Const QUOTE = ' 

        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("ThisWorkbook")
        Set CodeMod = VBComp.CodeModule

        With CodeMod
            LineNum = .CreateEventProc("Open", "Workbook")
            LineNum = LineNum + 1
            .InsertLines LineNum,  QUOTE 
        End With
    End Sub
4

2 回答 2

1

首先,请改变

Const QUOTE = '

对此:

Const QUOTE = "'"

基本上你的报价(或Rem)将是一个字符串,需要用引号引起来。

至于VB Extensibilty,您可能需要在找到该行后删除该行,并在开头插入带有注释的新行。

请参阅Chip Pearson:在 VBA 编辑器中编程

于 2013-04-25T15:05:47.493 回答
1

我已经找到了解决此代码问题的方法:

Sub CommentCodeModule(wb As Workbook)
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim WordToFind As String
        Dim SL As Long ' start line
        Dim EL As Long ' end line
        Dim SC As Long ' start column
        Dim EC As Long ' end column
        Dim Found As Boolean
        Dim LineNum As Long
        Set VBProj = wb.VBProject
        Set VBComp = VBProj.VBComponents("Module3")
        Set CodeMod = VBComp.CodeModule

        WordToFind = "test"

        With CodeMod
            SL = 1
            EL = .CountOfLines
            SC = 1
            EC = 255
            Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                EndLine:=EL, EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
            Do Until Found = False
                Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC)
                EL = .CountOfLines
                SC = EC + 1
                EC = 255

                Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                    EndLine:=EL, EndColumn:=EC, _
                    wholeword:=True, MatchCase:=False, patternsearch:=False)
            Loop
             LineNum = SL
             .DeleteLines LineNum

            .InsertLines LineNum, "  ' test"
        End With
    End Sub

感谢所有帮助过我的人。

于 2013-04-26T08:26:55.530 回答