Hiding Columns based on formula results

jhennige

New Member
Joined
May 1, 2003
Messages
27
I searched the site and found several similar questions, but I could not find the exact answer to my problem. I expect that this is an easy one.

I have a formula (similar to [=If(a1>b1,0,1)] in the protected cells F2:Q2 that will return either a 1 or a 0. I need a macro that will hide every column that contains a 0. Of course the macro needs to account for the formula and not just the value.

The data is dynamic so the macro needs to run if there are any changes to the formula results in F2:Q2.

The worksheet is protected with a password.

I am using Excel 2000 and Windows 2000.

Any help would be appreciated.
 
Hi,

The Change code would be pretty similar, something like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range, rngCell As Range

    If Target.Address <> "$C$30" Then Exit Sub
    'only action of there is a change to A2
    Set rng = Range("F2:Q2")
    Me.Unprotect password:="Letmein"
    'use your password here
    Application.ScreenUpdating = False
    rng.EntireColumn.Hidden = False
    'show all
    For Each rngCell In rng
        If rngCell.Value = 0 Then rngCell.EntireColumn.Hidden = True
        'hide if its a zero
    Next
    Me.Protect password:="Letmein"
    Application.ScreenUpdating = True
    Set rng = Nothing
    'free memory
End Sub
However, did you read the part in my previous post that said "... this will not prevent the UnDo problem as you are still using event macros." ?
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
That's the code I needed!

Yes, I did read your comment about it not preventing the undo problem, but since the cell that will launch the macro is not changed that often, the problem has been greatly reduced using the Worksheet_Change approach. With the Worksheet_Calculate approach, the Undo function was disabled altogether.

Once again, thank you!
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,336
Members
451,697
Latest member
pedroDH

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