Hide column - what is wrong with this code?

nutrastat

Board Regular
Joined
Nov 1, 2008
Messages
57
Hi,

Can somebody tell me what is wrong with the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("WK05_TRUE_FALSE").Value = False Then
Range("AI:AL").Selection.EntireColumn.Hidden = True
Else
Range("AI:AL").Selection.EntireColumn.Hidden = False
End If
End Sub

I have a cell called WK05_True_False which equals 'False' if it is NOT a 5 week period, and goes to 'True' if it is, as a month date on the front sheet is changed. When 'False' I want to hide columns AI:AL.

This macro is on the sheet to which it relates, but I keep getting

Run time error '1004'
Method Rage of object worksheet failed

Can anyone help - please?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Perhaps

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("WK05_TRUE_FALSE").Value = False Then
    Range("AI1:AL1").EntireColumn.Hidden = True
Else
    Range("AI1:AL1").EntireColumn.Hidden = False
End If
End Sub
 
Upvote 0
Sorry, still have the same error coming up.

Do you have any other ideas why the line...


If Range("WK05_TRUE_FALSE").Value = False Then
</pre>comes up as an error?
 
Upvote 0
Hmmm,
I sometimes get unwanted results with named ranges on other sheets...

Maybe you'd be better off using code in the change event in the named cell itself, on that sheet ...

PSEUDOCODE (Syntax is not reliable)
Code:
Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("WKSH_TRUE_FALSE").Address Then
     If Target.Value Then
        Worksheets("SheetX").Columns(XX).Hidden = True
    Else
        Worksheets("SheetX").Columns(XX).Hidden = False
    End If
End If

Otherwise, try fully qualifying the ranges with worksheets:
Worksheets("Sheet1").Range("MyRange").Value
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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