Help Streamling Hide Empty Cells Macro

chuckdanger

New Member
Joined
Apr 27, 2015
Messages
7
Hi, attempted to create a code to hide all "0" cell rows in a given column. These cells consist of a lookup seeking mail dates that have not occurred yet in data, so they are returning a 1/0/1900 value for the time being. Came up with the following code. I am not liking the run time on it and would appreciate input on how to make it more efficient. The column range "lr" never exceeds 1000, if that's a reference point.

Code:
Sub HideEmpties()

Dim lr As String


StartTime = Timer


Application.Calculation = xlManual = True


lr = Range("J" & Rows.Count).End(xlUp).Row


        Dim cell As Range
             For Each cell In Range("J1:J" + lr)
            cell.EntireRow.Hidden = cell.Value = 0
        Next cell
        
Application.Calculation = xlAutomatic = True


MinElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "This code ran successfully in " & MinElapsed & " minutes", vbInformation


End Sub
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I've heard that using Find is more efficient than For Each. You might try something like...

Code:
Sub FindMultipleOccurrences()

Dim rngSearch As Range, rngLast As Range, rngFound As Range
Dim strFirstAddress As String
Dim LastRow As Long

LastRow = Cells(Rows.Count, "J").End(xlUp).Row
Set rngSearch = ActiveSheet.Range("J1:J" & LastRow)
Set rngLast = rngSearch.Cells(rngSearch.Cells.Count)
Set rngFound = rngSearch.Find(What:=0, After:=rngLast, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If Not rngFound Is Nothing Then
    strFirstAddress = rngFound.Address
    Do
        Set rngFound = rngSearch.FindNext(rngFound)
        rngFound.EntireRow.Hidden = True
    Loop Until rngFound.Address = strFirstAddress
End If

End Sub

I'd be interested in seeing your timing results.

Oh, this looks for "0" - not 1/0/1900.

Cheers,

tonyyy
 
Upvote 0
Thanks, I will try and implement this, and keep you posted if I am seeing it go any quicker. Right now, I have it running 20-22 sec each tab for 21 tabs. :(
 
Upvote 0
In either case, you might shorten the run time by...

Inserting "Application.ScreenUpdating = False" just below the Sub statement, and

inserting "Application.ScreenUpdating = True" just above the End Sub statement.
 
Upvote 0
Thank You !! you have changed my life with those two lines. I ve seen it in use before, but never realized how good of a tool that is. The timer doesn't even have a chance to run 0:00:00. THANKS SO MUCH
 
Upvote 0
You're welcome. Glad I could help.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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