Excel

Remove All Comments from a VBA Project

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro will delete all comments from the VBA project of the active workbook. 

Discussion:

It may be necessary to remove all the comments from several macros in a VBA project. This macro will remove all comments from the VBA project of the active workbook. 

Code:

instructions for use

			

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

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code there.
  5. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Activate the workbook that you want to remove all the VBA comments from.
  2. Tools | Macro | Macros...
  3. Select Macro1 and press Run.
 

Sample File:

Remove Comments.zip 9.16KB 

Approved by mdmackillop


This entry has been viewed 255 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express