+ Reply to Thread
Results 1 to 2 of 2

Help deleting empty rows in all sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2011
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    25

    Help deleting empty rows in all sheets

    Just a beginner with VBA, trying to get this code to work in all sheets. Right now in only works in the active sheet. Ultimately it's supposed to delete all empty rows in all the worksheets.

    Public Sub DeleteCompletelyBlankRows() 
    
    Dim as worksheet
    
    For each WS in Worksheets 
    
    Dim R As Long
    Dim C As Range
    Dim N As Long
    Dim Rng As Range
    On Error GoTo EndMacro
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    If Selection.Rows.Count > 1 Then
    Set Rng = Selection
    Else
    Set Rng = ActiveSheet.UsedRange.Rows
    End If
    N = 0
    For R = Rng.Rows.Count To 1 Step -1
    If Application.WorksheetFunction.CountA(Rng.Rows(R).EntireRow) = 0 Then
    Rng.Rows(R).EntireRow.Delete
    N = N + 1
    End If
    Next R
    EndMacro:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    Any Help is greatly appreciated
    MZING81

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Help deleting empty rows in all sheets

    Hi there, i used your code, just made a few clean ups, give this a shot

    Public Sub DeleteCompletelyBlankRows()
    
    Dim WS As Worksheet
    Dim R As Integer
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    On Error GoTo EndMacro
    
    For Each WS In Worksheets
        Set Rng = WS.UsedRange.Rows
    
        For R = Rng.Rows.Count To 1 Step -1
            If Application.WorksheetFunction.CountA(Rng.Rows(R).EntireRow) = 0 Then
            Rng.Rows(R).EntireRow.Delete
            End If
        Next R
    Next WS
    
    EndMacro:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    you were missing the next WS function, and you were refering to activesheet instead of WS which meant anytime it went throught the loop it woudl refer to the same active sheet.

    let me know it works for you
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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