Macro to Hide Rows that contain specific Values...

diqbal

New Member
Joined
Jan 7, 2004
Messages
25
I need to create a macro that looks at every row in a worksheet and checks to see if columns C-H all have a value of "-" and if this is true it hides the row. It then keeps running for 9 more worksheets that are identical to the first one for all intents and purposes.

The logical reason for this is that I am creating a sheet that pulls in values from an external source and I want to hide all the rows that have no values so as to not have a lot of rows with just dashes in them.

Please help as I am an extreme newbie to Excel Macros. Thanks. :)
 
Howdy Nate. I like the use of .Find :-D But this too will error out if a user renames a sheet (which was why I stuck my nose back in on this one after Craig had offered a viable response). Question: I've not had any luck figuring out how to set up an array of worksheets referencing .CodeNames inside Array() like you can with .Names. Got any slick way of doing that other than one-by-one like I did?
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hello Greg,

See my edit, it uses index numbers eh. Of course this bombs out if someone moves the sheets! There's always a few ways of bolloxing up a nice application. :)

Otherwise, my advice would be to loop through the entire collection (using the collection, not integers) and have a contingency/contingencies, e.g., if ws.codename <> "SheetToSkip".

It's a simple, efficient procedure in terms of looping. :)
 
Upvote 0
Dang! And I really liked Nate's use of .Find, but this will hide any rows that have a "-", not just rows where all are "-". :-(
 
Upvote 0
Try changing xlPart to xlWhole eh. ;)

Have a go with the VBE Help File re: Find Method.
 
Upvote 0
Nah, that 'twern't me point. Diqbal needs 'em hidden if all the columns in [C:H] have a dash, not if any of 'em have a dash. And swapping LookAt to xlWhole actually makes it quit workin' unless ya switch LookIn to xlFormulas (which I don't get, but there is it).
 
Upvote 0
Nate,
I also was able to use your code. Very nice! Is there any way to alter the code to unhide rows if they no longer have "-" when re-run?
Brian
 
Upvote 0
Right, I missed that part of the quandary, how about:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> fubar()
<SPAN style="color:darkblue">Dim</SPAN> ws <SPAN style="color:darkblue">As</SPAN> Worksheet, cl <SPAN style="color:darkblue">As</SPAN> Range, OrigCl <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>, z <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>
Application.ScreenUpdating = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> ws <SPAN style="color:darkblue">In</SPAN> Worksheets(Array(1, 2))
    <SPAN style="color:darkblue">Set</SPAN> cl = ws.[c:c].Find("-", lookat:=xlWhole, LookIn:=xlValues)
    <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> cl <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
        <SPAN style="color:darkblue">Let</SPAN> OrigCl = cl.Address
        <SPAN style="color:darkblue">If</SPAN> WorksheetFunction.CountIf(cl.Resize(, 6), "-") = 6 <SPAN style="color:darkblue">Then</SPAN>
            cl.EntireRow.Hidden = <SPAN style="color:darkblue">True</SPAN>
            z = <SPAN style="color:darkblue">True</SPAN>
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
        Do: <SPAN style="color:darkblue">Set</SPAN> cl = ws.[c:c].FindNext(cl)
            <SPAN style="color:darkblue">If</SPAN> cl.Address = OrigCl <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Do</SPAN>
            <SPAN style="color:darkblue">If</SPAN> z <SPAN style="color:darkblue">Then</SPAN>
                OrigCl = cl.Address
                z = <SPAN style="color:darkblue">Not</SPAN> z
            <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
            <SPAN style="color:darkblue">If</SPAN> WorksheetFunction.CountIf(cl.Resize(, 6), "-") = 6 _
                <SPAN style="color:darkblue">Then</SPAN> cl.EntireRow.Hidden = <SPAN style="color:darkblue">True</SPAN>
        <SPAN style="color:darkblue">Loop</SPAN>
        <SPAN style="color:darkblue">Set</SPAN> cl = Nothing: <SPAN style="color:darkblue">Let</SPAN> OrigCl = <SPAN style="color:darkblue">Empty</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
Application.ScreenUpdating = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
</FONT>

I didn't follow your comment re: xlWhole, the above works fine for me...
 
Upvote 0
sarlo00 said:
Nate,
I also was able to use your code. Very nice! Is there any way to alter the code to unhide rows if they no longer have "-" when re-run?
Brian
Hello, thanks, just unhide the cells and run it again eh.

Code:
Sub dsfdfsd()
Dim ws As Worksheet
For Each ws In Sheets(Array(1, 2))
    ws.[a1:a65536].EntireRow.Hidden = False
Next
End Sub
 
Upvote 0
In testing Nate's new code (but still very nice Nate, not that you need me to tell you that) - I was able to get it to hang in an infinite loop if I had a row down the page that had just a dash in column c but not all columns. So here's a rework (with me being stubborn about codenames and not using ordinal positions either :wink: ).

<font face=Courier New>

<SPAN style="color:#00007F">Sub</SPAN> fubar2()

<SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, cl <SPAN style="color:#00007F">As</SPAN> Range, OrigCl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, z <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> ws(11) <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Set</SPAN> ws(1) = Sheet3
<SPAN style="color:#00007F">Set</SPAN> ws(2) = Sheet10
<SPAN style="color:#00007F">Set</SPAN> ws(3) = Sheet11
<SPAN style="color:#00007F">Set</SPAN> ws(4) = Sheet12
<SPAN style="color:#00007F">Set</SPAN> ws(5) = Sheet13
<SPAN style="color:#00007F">Set</SPAN> ws(6) = Sheet14
<SPAN style="color:#00007F">Set</SPAN> ws(7) = Sheet15
<SPAN style="color:#00007F">Set</SPAN> ws(8) = Sheet16
<SPAN style="color:#00007F">Set</SPAN> ws(9) = Sheet17
<SPAN style="color:#00007F">Set</SPAN> ws(10) = Sheet18
<SPAN style="color:#00007F">Set</SPAN> ws(11) = Sheet8

Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(ws)
    <SPAN style="color:#00007F">Set</SPAN> cl = ws(i).[c:c].Find("-", lookat:=xlPart, LookIn:=xlValues)
    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> cl <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Let</SPAN> OrigCl = cl.Address
        <SPAN style="color:#00007F">If</SPAN> WorksheetFunction.CountIf(cl.Resize(, 6), "-") = 6 <SPAN style="color:#00007F">Then</SPAN>
            cl.EntireRow.Hidden = <SPAN style="color:#00007F">True</SPAN>
            z = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        Do: <SPAN style="color:#00007F">Set</SPAN> cl = ws(i).[c:c].FindNext(cl)
            <SPAN style="color:#00007F">If</SPAN> cl <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN>
            <SPAN style="color:#00007F">If</SPAN> cl.Address = OrigCl <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Do</SPAN>
            <SPAN style="color:#00007F">If</SPAN> WorksheetFunction.CountIf(cl.Resize(, 6), "-") = 6 <SPAN style="color:#00007F">Then</SPAN>
                cl.EntireRow.Hidden = <SPAN style="color:#00007F">True</SPAN>
            <SPAN style="color:#00007F">ElseIf</SPAN> z <SPAN style="color:#00007F">Then</SPAN>
                OrigCl = cl.Address
                z = <SPAN style="color:#00007F">False</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">Loop</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> cl = Nothing: <SPAN style="color:#00007F">Let</SPAN> OrigCl = <SPAN style="color:#00007F">Empty</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> ws(i) = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">Next</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0

Forum statistics

Threads
1,222,703
Messages
6,167,743
Members
452,135
Latest member
Lugen

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