+ Reply to Thread
Results 1 to 11 of 11

Clunky Slow Code, Hides Empty Rows, Takes too long...

Hybrid View

  1. #1
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Clunky Slow Code, Hides Empty Rows, Takes too long...

    Hello, I've written code that hides rows on different sheets if they are empty and in one case changes column width to keep the formatting looking ok. I've used non-contiguous named ranges because there are multiple places on each row that could (or could not) be empty. It does exactly what I want, but it takes a long time to process and will only get longer if I continue to add new sheets. I'm pretty new to VBA, but I know I didn't write it well as I am iterating through multiple ranges and performing tests on each cell. I'm pretty sure I could use an array, but I failed at coding that myself. Moreover, using xlspecialcellblanks isn't working, because the "empty" cells have formulas in them. Any help would be greatly appreciated! Thanks very much.
    Sub F2F4HideRows()
    
    Application.ScreenUpdating = False
    
    Dim rng As Range
    
    Sheets("F2").Range("F2HideRows").Rows.EntireRow.Hidden = True
    Sheets("F4").Range("F4HideRows").Rows.EntireRow.Hidden = True
    Sheets("F5").Range("F5HideRows").Rows.EntireRow.Hidden = True
    Sheets("F6").Range("F6HideRows").Rows.EntireRow.Hidden = True
    Sheets("F4").Columns("C").ColumnWidth = 17
    Sheets("F4").Columns("D").ColumnWidth = 17
    Sheets("F4").Columns("F").ColumnWidth = 17
    
    On Error Resume Next
    
    For Each rng In Sheets("F2").Range("F2HideRows")
    
        If rng <> Empty Then
        
        rng.Rows.EntireRow.Hidden = False
        
        End If
        
    Next
    
    For Each rng In Sheets("F4").Range("F4HideRows")
    
    If rng <> Empty Then
        
        rng.Rows.EntireRow.Hidden = False
        Sheets("F4").Columns("C").ColumnWidth = Sheets("F4").Columns("C").ColumnWidth + 0.3
        Sheets("F4").Columns("D").ColumnWidth = Sheets("F4").Columns("D").ColumnWidth + 0.3
        Sheets("F4").Columns("F").ColumnWidth = Sheets("F4").Columns("F").ColumnWidth + 0.3
        
        End If
        
    Next
    
    For Each rng In Sheets("F5").Range("F5HideRows")
    
        If rng <> Empty Then
        
        rng.Rows.EntireRow.Hidden = False
        
        End If
        
    Next
    
    For Each rng In Sheets("F6").Range("F6HideRows")
    
        If rng <> Empty Then
        
        rng.Rows.EntireRow.Hidden = False
        
        End If
        
    Next
    
    On Error GoTo 0
    
    Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Try replacing:
    For Each rng In Sheets("F2").Range("F2HideRows")
    
        If rng <> Empty Then
        
        rng.Rows.EntireRow.Hidden = False
        
        End If
        
    Next

    with:
    With Range("F2HideRows").SpecialCells(xlCellTypeBlanks)
        .EntireRow.Hidden = True
    End With

    Any better?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Ah - ignore my post, I just read your post PROPERLY Silly me!

  4. #4
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Oh well, thanks for trying anyways. I was thinking maybe there was an array solution, so I could reference the array index instead of each cell but I don't know how to do it. I know I could get the range to be hidden set dynamically to a variable but then I couldn't get the column width part to work too.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Brilliant Question by the way. It got me thinking.

    This will un-hide rows that contain numbers or text, whether they are constants or returned by a formula.

    No loops are involved.

    Enjoy.

  6. #6
    Registered User
    Join Date
    11-08-2013
    Location
    Sacramento, California
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Hi mehmetcik,

    Thanks so much for your reply! Unfortunately, I want to hide rows that have formulas (or references) but are returning blanks due to no input in the reference cell. Your code unhides these which are a reference to empty cells. I tried IF(ref="","",ref) in the cells as well and still no dice. I think that is why the "Empty" property has worked in my code. I think I may have to set the area to be hidden first by a loop and then hide that. I'm really new with arrays, and I got one that seems to iterate through an index of cells, but for some reason (even though I can see it has the correct reference in debug) it doesn't recognize the cell... maybe I need to redim the array, but again, I am inexperienced with them.

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Hi

    try this code.

    
    Sub UnhideMacro()
    
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, xlNumbers + xlTextValues).Select
    Selection.EntireRow.Hidden = False
    ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues).Select
    Selection.EntireRow.Hidden = False
    End Sub

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Clunky Slow Code, Hides Empty Rows, Takes too long...

    Hi

    All you have to do is to hide the formulas returning blanks I would have thought.

+ 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. VBA that hides rows with 0 takes FOREVER to run
    By Sky188 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-28-2013, 04:04 PM
  2. [SOLVED] Worksheet Macro that hides empty rows does not work
    By Taktiker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2012, 11:11 AM
  3. Long code takes an age to run
    By E3iron in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-05-2009, 07:38 AM
  4. My Code takes too long to execute
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2008, 12:12 AM
  5. macro that hides rows run slow
    By yoav_b in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-15-2007, 11:16 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