delete comment

R

Rob van Gelder

Sounds fun.

I just put this together.

You will need to add a reference to "Microsoft Visual Basic for Applications
Extensibility"


It handles string escape character and line continuations. Not really sure
what else I should account for...


Sub test()
Dim vbc As VBComponent, i As Long, j As Long, str As String
Dim blnStringMode As Boolean, blnLineContinue As Boolean

For Each vbc In ThisWorkbook.VBProject.VBComponents
blnStringMode = False
i = 1
Do Until i > vbc.CodeModule.CountOfLines
str = vbc.CodeModule.Lines(i, 1)
blnLineContinue = (Right(str, 2) = " _")
For j = 1 To Len(str)
Select Case Mid(str, j, 1)
Case """": blnStringMode = Not blnStringMode
Case "'"
If Not blnStringMode Then
str = RTrim(Mid(str, 1, j - 1))
If LTrim(str) = "" Then
vbc.CodeModule.DeleteLines i
i = i - 1
Else
vbc.CodeModule.ReplaceLine i, str
End If
If blnLineContinue Then
i = i + 1
Do
str = vbc.CodeModule.Lines(i, 1)
blnLineContinue = (Right(str, 2) = " _")
vbc.CodeModule.DeleteLines i
Loop While blnLineContinue
i = i - 1
End If
End If
End Select
Next
i = i + 1
Loop
Next
End Sub
 
M

Mark

Hi Rob!
Thanks for help!
Regards
Mark
-----Original Message-----
Sounds fun.

I just put this together.

You will need to add a reference to "Microsoft Visual Basic for Applications
Extensibility"


It handles string escape character and line continuations. Not really sure
what else I should account for...


Sub test()
Dim vbc As VBComponent, i As Long, j As Long, str As String
Dim blnStringMode As Boolean, blnLineContinue As Boolean

For Each vbc In ThisWorkbook.VBProject.VBComponents
blnStringMode = False
i = 1
Do Until i > vbc.CodeModule.CountOfLines
str = vbc.CodeModule.Lines(i, 1)
blnLineContinue = (Right(str, 2) = " _")
For j = 1 To Len(str)
Select Case Mid(str, j, 1)
Case """": blnStringMode = Not blnStringMode
Case "'"
If Not blnStringMode Then
str = RTrim(Mid(str, 1, j - 1))
If LTrim(str) = "" Then
vbc.CodeModule.DeleteLines i
i = i - 1
Else
vbc.CodeModule.ReplaceLine i, str
 
R

Rob van Gelder

Having taken another look at my code, I see some room for improvement:

Sub test()
Dim vbc As VBComponent, i As Long, j As Long, str As String
Dim blnStringMode As Boolean, blnLineContinue As Boolean

For Each vbc In ThisWorkbook.VBProject.VBComponents
blnStringMode = False
i = 1
Do Until i > vbc.CodeModule.CountOfLines
str = vbc.CodeModule.Lines(i, 1)
blnLineContinue = (Right(str, 2) = " _")
For j = 1 To Len(str)
Select Case Mid(str, j, 1)
Case """": blnStringMode = Not blnStringMode
Case "'"
If Not blnStringMode Then
str = RTrim(Mid(str, 1, j - 1))
If LTrim(str) = "" Then
vbc.CodeModule.DeleteLines i
i = i - 1
Else
vbc.CodeModule.ReplaceLine i, str
End If
Do While blnLineContinue
blnLineContinue =
(Right(vbc.CodeModule.Lines(i + 1, 1), 2) = " _")
vbc.CodeModule.DeleteLines i + 1
Loop
Exit For
End If
End Select
Next
i = i + 1
Loop
Next
End Sub
 
Top