Thread: delete comment
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rob van Gelder[_4_] Rob van Gelder[_4_] is offline
external usenet poster
 
Posts: 1,236
Default delete comment

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


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Mark" wrote in message
...
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
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



--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Mark" wrote in message
...
Is there any verify vba to delete all comments in one
project VBE?

Regards
Mark



.