[VBA] clean up empty code line in VBE under Sub / Function

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows
Very Often I create many blank codes lines during VBA coding.

Is there available add-in to clean up those empty lines, to generate a more neatly-aligned VBA code?


Thanks!
e.g.
Code:
Sub XYZ()
 Dim A as Integer
 (empty)
 (empty)
 A = A + 1
 (empty)
 MsgBox A
 (empty)
 (empty)
End Sub


Thanks a lot!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Though not an add-in, you can use the following to delete empty code lines...

Code:
Sub DeleteBlankLinesInVBE()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This deletes blank code lines in the specified module
' And also counts the total number of lines
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim N As Long
Dim S As String
Dim LineCount As Long
Dim wb As String
Dim Answer As String
Dim VBComp As VBIDE.VBComponent

wb = InputBox(prompt:="Please enter a workbook name.", Title:="Delete Blank Lines", Default:="Book1")
Answer = InputBox(prompt:="Please enter a module number.", Title:="Delete Blank Lines", Default:="Module1")
Set VBComp = Workbooks(wb).VBProject.VBComponents(Answer)

With VBComp.CodeModule
    For N = .CountOfLines To 1 Step -1
        S = .Lines(N, 1)
        If Trim(S) = vbNullString Then
            .DeleteLines N
        ElseIf Left(Trim(S), 1) = "'" Then
            ' comment line, skip it
        Else
            LineCount = LineCount + 1
        End If
    Next N
End With

Debug.Print LineCount - 1
End Sub

Cheers,

tonyyy
 
Upvote 0
Fazza - Just copied it from my personal.xlsb; something I use when encountering posts with lots of blank lines. ;-)
 
Upvote 0
i have some code for indenting. but it doesn't currently delete blank lines. maybe I'll add that

one comment from my code, relevant here, is

' REQUIRED REFERENCE : Microsoft Visual Basic for Applications Extensibility 5.3
 
Upvote 0
Guys, thank you so much!
How long have you been immersed in using Excel VBA?
Hope one day I create such codes myself.
Thanks a lot!
 
Upvote 0
The code works well!
One question. is it possible to create a shortcut key for it, such that, during VBE coding, it can be executed upon triggering the shortcut keys?

Thanks!
 
Upvote 0
The code works well!
One question. is it possible to create a shortcut key for it, such that, during VBE coding, it can be executed upon triggering the shortcut keys?

Thanks!

You're welcome...

Sorry, don't know about creating a shortcut key in the vbe. You could, I suppose, add an icon to the QAT and launch it from there.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

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