从另一个 VBA 代码注释 vba 代码
Posted
技术标签:
【中文标题】从另一个 VBA 代码注释 vba 代码【英文标题】:Comment vba code from another VBA CODE 【发布时间】:2013-04-25 14:51:30 【问题描述】:我想从另一个代码 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
【问题讨论】:
请出示您已有的代码sn-p 【参考方案1】:首先,请更改
Const QUOTE = '
到这里:
Const QUOTE = "'"
基本上你的报价(或Rem
)是一个字符串,需要用引号括起来。
对于VB Extensibilty,您可能需要在找到后删除该行,并在开头插入一个带有注释的新行。
见Chip Pearson: Programming in the VBA Editor
【讨论】:
【参考方案2】:我已经找到了解决此代码问题的方法:
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
感谢所有帮助过我的人。
【讨论】:
以上是关于从另一个 VBA 代码注释 vba 代码的主要内容,如果未能解决你的问题,请参考以下文章
求高手帮忙注释下excel中vba宏代码各是啥意思,满意的加满分,谢谢!
如何在 MS Access VBA 上将文本框注释值输入设置为表格数据插入