[VBA ] 請問去除所有 module comments 的原理

作者: gecer (gecer)   2014-10-19 18:31:40
小弟不太懂下面這一段code的原理 請好心大大賜教
http://www.vbaexpress.com/kb/getarticle.php?kb_id=266
Option Explicit
Sub Macro1()
Dim n As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim LineText As String
Dim ExitString As String
Dim Quotes As Long
Dim q As Long
Dim StartPos As Long
For i = 1 To ActiveWorkbook.VBProject.VBComponents.Count
With ActiveWorkbook.VBProject.VBComponents(i).CodeModule
For j = .CountOfLines To 1 Step -1
LineText = Trim(.Lines(j, 1))
If LineText = "ExitString = " & _
"""" & "Ignore Comments In This Module" & """" Then
Exit For
End If
StartPos = 1
Retry:
n = InStr(StartPos, LineText, "'")
q = InStr(StartPos, LineText, """")
Quotes = 0
If q < n Then
For l = 1 To n
If Mid(LineText, l, 1) = """" Then
Quotes = Quotes + 1
End If
Next l
End If
If Quotes = Application.WorksheetFunction.Odd(Quotes) Then
StartPos = n + 1
Goto Retry:
Else
Select Case n
Case Is = 0
Case Is = 1
.DeleteLines j, 1
Case Is > 1
.ReplaceLine j, Left(LineText, n - 1)
End Select
End If
Next j
End With
Next i
ExitString = "Ignore Comments In This Module"
End Sub
作者: MOONRAKER (㊣牛鶴鰻毛人)   2014-10-20 21:49:00
你知道 ' 在 VB 裡的用途吧隨便找個10列的VB程式 手動跑一遍不就知道了

Links booklink

Contact Us: admin [ a t ] ucptt.com