+ Reply to Thread
Results 1 to 3 of 3

Remove blank lines of code

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Remove blank lines of code

    Hello everybody
    I need a macro that enables me to clear the lines of code (blank of course)
    I have very long code which has blank lines in between ..
    I need a quick way to clear these blank lines so as to be easy to read and I can check it carefully
    Doing that task manually is very boring process specially with long macros

    For example:
    Sub Example()
    
    
    MsgBox "Hello"
    
    
    
    Range("A1").Value = "ExcelForum"
    
    
    
    Debug.Print "Thank you"
    
    
    
    End Sub
    would be
    Sub Example()
    MsgBox "Hello"
    Range("A1").Value = "ExcelForum"
    Debug.Print "Thank you"
    End Sub
    Last edited by YasserKhalil; 08-22-2015 at 04:59 AM.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Remove blank lines of code

    use with caution as it will delete original contents of module

    Sub StripBlankLines()
    
        Dim Code As Object  'CodeModule
        Dim Text As String
        Dim Index As Long
        Dim NewText As String
        
        Set Code = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
        For Index = 1 To Code.CountOfLines
            If Len(Trim(Code.Lines(Index, 1))) > 0 Then
                NewText = NewText & Code.Lines(Index, 1) & vbLf
            End If
        Next
        Debug.Print NewText
        Code.DeleteLines 1, Code.CountOfLines
        Code.AddFromString NewText
        
    End Sub
    put code in a new module within vbproject. change module name to suit
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Remove blank lines of code

    Mr. Andy Pope The Genius
    Thanks a lot for this perfect solution.
    Best Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Remove blank lines
    By ReeceB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-01-2015, 06:23 PM
  2. [SOLVED] remove lines if a cell is blank
    By Christopherdj in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-01-2013, 10:08 PM
  3. How do I remove blank lines in the first column.
    By SWMagic in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-29-2013, 05:14 AM
  4. Create List and remove blank Lines
    By jamjam200 in forum Excel General
    Replies: 0
    Last Post: 01-22-2013, 12:30 PM
  5. Remove Blank Lines
    By Robotacha2010 in forum Excel General
    Replies: 5
    Last Post: 08-16-2010, 11:59 AM
  6. Remove Blank Lines
    By Nigel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2005, 11:00 AM
  7. remove blank lines from an entire spreadsheet
    By mdeanda in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-21-2005, 09:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1