+ Reply to Thread
Results 1 to 8 of 8

Can't delete hidden rows in bulk

Hybrid View

forrestgump1980 Can't delete hidden rows in... 08-03-2015, 09:46 AM
bmouse Re: Can't delete hidden rows... 08-03-2015, 09:56 AM
StuCram Re: Can't delete hidden rows... 08-03-2015, 10:12 AM
forrestgump1980 Re: Can't delete hidden rows... 08-03-2015, 10:22 AM
TMS Re: Can't delete hidden rows... 08-03-2015, 10:31 AM
JOHN H. DAVIS Re: Can't delete hidden rows... 08-03-2015, 10:46 AM
Crispy85 Re: Can't delete hidden rows... 08-03-2015, 10:48 AM
TMS Re: Can't delete hidden rows... 08-03-2015, 11:03 AM
  1. #1
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    181

    Can't delete hidden rows in bulk

    Hi there,

    Is there any way I can delete hidden rows in bulk. I wrote some code to do this line by line in a loop, however, this was way to slow as there are over 25,000 rows of data in my spread sheet.

    Any help would be greatly appreciated.

    Regards,

    Forrestgump1980

  2. #2
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: Can't delete hidden rows in bulk

    Perhaps it is because of the code being too complex?
    I have a code of my own, care to try?

    Sub fff()
    
    Dim StartNumber As Integer
    Dim EndNumber As Long
    
    EndNumber = 25000
    
    For StartNumber = 1 To EndNumber
    
        If Rows(StartNumber).Hidden = True Then
        Rows(StartNumber).Delete
        Else
        End If
        
    Next StartNumber
        
    
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Can't delete hidden rows in bulk

    A potential problem with the solution by 'bmouse' is that the rows are renumbered whenever one of them is deleted.
    If row 9 is hidden, it gets deleted and then row 10 becomes row 9 but the loop will move on to the new row 10, skipping the previous row 10 which is now row 9. Does that make sense?

    The cure for this is to work backwards or from the bottom up.

    It will be speedier too when screen updating is turned off

    Sub Delete_Hidden_Rows()
      Application.ScreenUpdating = False
      Dim rowNum as Long
    
      For rowNum = 25000 to 1 step -1
        If Rows(rowNum).Hidden = True Then 
          Rows(rowNum).Delete
        End If
      Next rowNum
    
      Application.ScreenUpdating = True
    End Sub
    If this has been helpful, please click on the star at the left.

  4. #4
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    181

    Re: Can't delete hidden rows in bulk

    Hi guys,

    Thanks for this but unfortunately I have tried this method already and it proves very slow when working with such a large number of rows (Over 25,000) therefore was trying to find a way of doing it quickly in bulk.

    Regards,

    Forrestgump1980

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,500

    Re: Can't delete hidden rows in bulk

    Maybe this:

    Option Explicit
    
    Sub sDeleteHiddenRows()
    
    Dim rAll As Range, rVisible As Range, rDelete As Range, cell As Range
    Set rAll = Range("A1:A100")
    Set rVisible = Range("A1:A100").SpecialCells(xlCellTypeVisible)
    
    Application.ScreenUpdating = False
    For Each cell In rAll
        If Intersect(cell, rVisible) Is Nothing Then
            If rDelete Is Nothing Then
                Set rDelete = cell
            Else
                Set rDelete = Union(rDelete, cell)
            End If
        End If
    Next cell
    
    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
    Application.ScreenUpdating = True
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Can't delete hidden rows in bulk

    Maybe:

    Sub forrestgump1980()
    Dim x As Long, y As Long
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    y = ActiveSheet.UsedRange.Columns.Count
    x = Range("A" & Rows.Count).End(3).row
    With Range(Cells(1, y + 1), Cells(x, y + 1))
        .SpecialCells(12).Value = "Keep"
        .EntireRow.Hidden = False
        .AutoFilter 1, "<>Keep", xlWhole
        .SpecialCells(12).EntireRow.Delete
        .Delete
    End With
    ActiveSheet.AutoFilterMode = False
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    End Sub
    Last edited by JOHN H. DAVIS; 08-03-2015 at 10:48 AM.

  7. #7
    Forum Contributor
    Join Date
    06-14-2013
    Location
    jk
    MS-Off Ver
    Excel 2007
    Posts
    326

    Re: Can't delete hidden rows in bulk

    TMS Solution would work well.
    Or perhaps copy the visible cells (with SpecialCells(xlCellTypeVisible) and paste in a new sheet.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,500

    Re: Can't delete hidden rows in bulk

    I think John's solution would work well for removing the rows in place, and Crispy85's suggestion would be good if you want to move the data to another sheet.

    Be interesting to time the different approaches.

    Regards, TMS

+ 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. Replies: 10
    Last Post: 06-07-2018, 04:16 PM
  2. Replies: 1
    Last Post: 04-08-2014, 02:50 AM
  3. Any faster way to delete hidden rows of data
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2012, 02:58 PM
  4. Replies: 4
    Last Post: 06-11-2010, 03:29 PM
  5. delete all blank rows on a hidden sheet
    By FRIEL in forum Excel General
    Replies: 1
    Last Post: 09-10-2008, 12:32 PM
  6. Delete hidden rows & columns
    By tommy_gtr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-04-2007, 06:12 AM
  7. Delete Hidden Rows
    By Makeda in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 05-18-2005, 03:03 PM

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