这个是罗老师的代码 Sub 删除批注() Dim oCodeMod As CodeModule, ProcType As Long, 起始行 As Long, 结束行 As Long Dim 过程名称 As String, 光标行 As Long, Item As Long, 注释位置 As Long, CodeRow As String Set oCodeMod = Application.VBE.ActiveCodePane.CodeModule Application.VBE.ActiveCodePane.GetSelection 光标行, 0, 0, 0 '获取选区第一行行号 过程名称 = oCodeMod.ProcOfLine(光标行, ProcType) '获区过程名称 If 过程名称 <> "" Then 起始行 = oCodeMod.ProcStartLine(过程名称, ProcType) '获取过程的起止行行号 结束行 = (起始行 + oCodeMod.ProcCountLines(过程名称, ProcType)) - 1 For Item = 结束行 To 起始行 Step -1 '遍历每一行 CodeRow = oCodeMod.Lines(Item, 1) '提取一行的代码 If Len(Trim(CodeRow)) > 0 Then '如果该行长度大于0 注释位置 = InStr(1, CodeRow, "'") '计算单引号的位置 If 注释位置 > 0 Or InStr(1, CodeRow, "Rem") > 0 Then '如果有单引号出现或者Rem出现 '如果第一字符就是单引号,替换成空,否由通过"去批注"函数获取其注释以外的部分 If 注释位置 = 1 Then CodeRow = "" Else CodeRow = 去批注(CodeRow) If Left(Replace(CodeRow, " ", ""), 3) = "Rem" Then CodeRow = "" '如果左边是Rem则替换成空 oCodeMod.ReplaceLine Item, CodeRow '执行替换 End If End If Next End If End Sub |