Remove all blank lines in VBE code editor for all projects

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
I need support code that removes all blank lines in VBE editor for all projects including ( Userform, module, Sheet, ThisWorkbook ) . Due to a lot of code, I can't do it manually. Please everyone help. I sincerely thank the forum

1631546667438.png
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Alternatively, this can also be done with a 3rd party tool. For example, the relatively inexpensive AET VBE Tools add-in (which used to be advertised here, because that's how I came to buy it) has a nice feature for this. Plus you can choose between deleting all blank lines, and reducing multiple blank lines to just one - which I prefer.


Also, a lot of code editors support that feature. For example, I have EditPad Pro and I can just paste the code into the editor, remove the blank lines, and paste it back into the VBE.
 
Upvote 0
The code below does what you want. Initially any comment is removed. Removing indents and blank lines is optional.
Note that you have to enable access to VBA projects within Excel's Trust Centre.
Also note the dummy procedure, which should be placed at the bottom of every code module in order to be skipped.

VBA Code:
Option Explicit

Public Sub CleanVBAModules()

    DeleteCommentsFromVBAModules ThisWorkbook, True, True

End Sub

Private Sub DeleteCommentsFromVBAModules(ByVal argWb As Workbook, Optional ByVal RemoveIndent As Boolean = False, Optional ByVal RemoveBlankLines As Boolean = False)

    Dim LineText As String, TempText As String
    Dim LineNumber As Long, CmmtPos As Long
    Dim HasLineBreak As Boolean, LineBreaks As Long
    Dim i As Long

    For i = 1 To argWb.VBProject.VBComponents.Count
        With argWb.VBProject.VBComponents(i).CodeModule
            For LineNumber = .CountOfLines To 1 Step -1

                LineText = IIf(RemoveIndent, Trim$(.Lines(LineNumber, 1)), .Lines(LineNumber, 1))
                If Not Trim$(LineText) = "DO_NOT_TOUCH = " & """" & "DO_NOT_TOUCH" & """" Then

                    HasLineBreak = GetLineBreaks(LineText, LineBreaks)
                    CmmtPos = GetCmmtPos(LineText, 1)

                    Select Case CmmtPos
                    Case 0
                        If Trim$(LineText) = vbNullString And RemoveBlankLines Then
                            .DeleteLines LineNumber, 1
                        Else
                            If Len(LineText) > 0 And RemoveIndent Then
                                .ReplaceLine LineNumber, LineText
                            End If
                        End If
                    Case 1
                        If HasLineBreak Then
                            .DeleteLines LineNumber + 1, LineBreaks
                            LineBreaks = 0
                        End If
                        .DeleteLines LineNumber, 1
                    Case Else
                        TempText = Left(LineText, CmmtPos - 1)
                        If Len(Trim$(TempText)) > 0 Then
                            .ReplaceLine LineNumber, TempText
                        Else
                            .DeleteLines LineNumber, 1
                        End If
                        If HasLineBreak Then
                            .DeleteLines LineNumber + 1, LineBreaks
                            LineBreaks = 0
                        End If
                    End Select
                Else
                    Exit For
                End If
            Next LineNumber
        End With
    Next i
End Sub

Private Function GetLineBreaks(ByRef argText As String, ByRef argLineBreaks As Long) As Boolean
    If Right$(argText, 1) = "_" Then
        argLineBreaks = argLineBreaks + 1
        GetLineBreaks = True
    Else
        argLineBreaks = 0
    End If
End Function

Private Function GetCmmtPos(ByRef argText As String, ByVal argSearchPos As Long) As Long
    Dim DQPos As Long
    GetCmmtPos = InStr(argSearchPos, argText, "'")
    If GetCmmtPos > 0 Then
        DQPos = InStr(argSearchPos, argText, """")
        If DQPos > 0 Then
            If GetCmmtPos > DQPos Then
                DQPos = InStr(GetCmmtPos, argText, """")
                If DQPos > 0 Then
                    GetCmmtPos = GetCmmtPos(argText, DQPos)
                End If
            End If
        End If
    End If
End Function

Private Sub DO_NOT_TOUCH_DUMMY_SUB()
Dim DO_NOT_TOUCH As String
DO_NOT_TOUCH = "DO_NOT_TOUCH"
End Sub
 
Upvote 0
The code below does what you want. Initially any comment is removed. Removing indents and blank lines is optional.
Note that you have to enable access to VBA projects within Excel's Trust Centre.
Also note the dummy procedure, which should be placed at the bottom of every code module in order to be skipped.

VBA Code:
Option Explicit

Public Sub CleanVBAModules()

    DeleteCommentsFromVBAModules ThisWorkbook, True, True

End Sub

Private Sub DeleteCommentsFromVBAModules(ByVal argWb As Workbook, Optional ByVal RemoveIndent As Boolean = False, Optional ByVal RemoveBlankLines As Boolean = False)

    Dim LineText As String, TempText As String
    Dim LineNumber As Long, CmmtPos As Long
    Dim HasLineBreak As Boolean, LineBreaks As Long
    Dim i As Long

    For i = 1 To argWb.VBProject.VBComponents.Count
        With argWb.VBProject.VBComponents(i).CodeModule
            For LineNumber = .CountOfLines To 1 Step -1

                LineText = IIf(RemoveIndent, Trim$(.Lines(LineNumber, 1)), .Lines(LineNumber, 1))
                If Not Trim$(LineText) = "DO_NOT_TOUCH = " & """" & "DO_NOT_TOUCH" & """" Then

                    HasLineBreak = GetLineBreaks(LineText, LineBreaks)
                    CmmtPos = GetCmmtPos(LineText, 1)

                    Select Case CmmtPos
                    Case 0
                        If Trim$(LineText) = vbNullString And RemoveBlankLines Then
                            .DeleteLines LineNumber, 1
                        Else
                            If Len(LineText) > 0 And RemoveIndent Then
                                .ReplaceLine LineNumber, LineText
                            End If
                        Kết thúc nếu
                    Trường hợp 1
                        Nếu HasLineBreak thì
                            .DeleteLines LineNumber + 1, LineBreaks
                            LineBreaks = 0
                        Kết thúc nếu
                        .DeleteLines LineNumber, 1
                    Trường hợp khác
                        TempText = Left (LineText, CmmtPos - 1)
                        Nếu Len (Trim $ (TempText))> 0 Thì
                            .ReplaceLine LineNumber, TempText
                        Khác
                            .DeleteLines LineNumber, 1
                        Kết thúc nếu
                        Nếu HasLineBreak thì
                            .DeleteLines LineNumber + 1, LineBreaks
                            LineBreaks = 0
                        Kết thúc nếu
                    Kết thúc Lựa chọn
                Khác
                    Thoát cho
                Kết thúc nếu
            Số dòng tiếp theo
        Kết thúc với
    Tiếp theo tôi
Kết thúc Sub

Hàm riêng GetLineBreaks (ByRef argText As String, ByRef argLineBreaks As Long) As Boolean
    Nếu Đúng $ (argText, 1) = "_" Thì
        argLineBreaks = argLineBreaks + 1
        GetLineBreaks = Đúng
    Khác
        argLineBreaks = 0
    Kết thúc nếu
Chức năng kết thúc

Hàm riêng GetCmmtPos (ByRef argText As String, ByVal argSearchPos As Long)
    Dim DQPos As Long
    GetCmmtPos = InStr (argSearchPos, argText, "'")
    Nếu GetCmmtPos> 0 Thì
        DQPos = InStr (argSearchPos, argText, "" "")
        Nếu DQPos> 0 Thì
            Nếu GetCmmtPos> DQPos thì
                DQPos = InStr (GetCmmtPos, argText, "" "")
                Nếu DQPos> 0 Thì
                    GetCmmtPos = GetCmmtPos (argText, DQPos)
                Kết thúc nếu
            Kết thúc nếu
        Kết thúc nếu
    Kết thúc nếu
Chức năng kết thúc

Sub riêng DO_NOT_TOUCH_DUMMY_SUB ()
Dim DO_NOT_TOUCH thành chuỗi
DO_NOT_TOUCH = "DO_NOT_TOUCH"
Kết thúc Sub

[/MÃ SỐ]
[/QUOTE]
Mã chạy chính xác như tôi dự định. tôi cảm ơn bạn rất nhiều
 
Upvote 0
I am Vietnamese. Let me ask if I want to run the above code and apply it to another file (currently the code is applied to the project containing the code), how can I edit it. That is, when running the code, it will show the path to select the file to be executed
You are welcome and thanks for letting me know (happy with Google Translate ;) )
 
Upvote 0
You can access the VBA project of another workbook in several ways.
  1. In the code (procedure CleanVBAmodules) change the first parameter (ThisWorkbook) by the ActiveWorkbook object. Then open the workbook to be edited, press ALT F8 (Macros dialog) and run the CleanVBAmodules procedure.
  2. Declare a Workbook variable and assign a workbook to it, either by a) including the path and filename in your code and then opening the workbook, or b) by using a FileDialog.
See examples below.

VBA Code:
    ' = = = = Example 1 = = = = = = = = = = = = = = = = = = = = = =
    
    DeleteCommentsFromVBAModules ActiveWorkbook, True, True


    ' = = = = Example 2a = = = = = = = = = = = = = = = = = = = = = =

    Dim oWb As Workbook, FullName As String

    ' open workbook directly using hardcoded file path and name
    FullName = "C:\Users\Excelpromax123\Documents\SomeWorkbook.xlsm"
    Set oWb = Application.Workbooks.Open(FullName)

    ' access VBA project of that workbook
    If Not oWb Is Nothing Then
        DeleteCommentsFromVBAModules oWb, True, True
    End If


    ' = = = = Example 2b = = = = = = = = = = = = = = = = = = = = = =

   Dim oWb As Workbook, FullName As String

    ' open workbook through file open dialog
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel Macro Workbooks", "*.xlsm"
        .Filters.Add "All Files", "*.*"
        .FilterIndex = 1
        .Show
        If .SelectedItems.Count > 0 Then
            FullName = .SelectedItems(1)
            Set oWb = Application.Workbooks.Open(FullName)
            
            ' access VBA project of that workbook
            DeleteCommentsFromVBAModules oWb, True, True
            
        Else
            ' cancel was pressed, do nothing
        End If
    End With
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top