Worksheet Macro, Change Based on Cell Data, Hide or Show Columns on Different Worksheets

teksp0rt

New Member
Joined
Feb 6, 2008
Messages
33
Hello Experts. Can you help me with this Macro please?

I am trying to hide a range of columns and rows on a separate worksheet (Worksheet "R"), when the value of a cell in D8 changes on Worksheet "A" - the sheet where I modify the value. I have been trying different code but cannot get the section in red below to work correctly, which hides or unhides a column on Worksheet R... For reference, I have shortened my Macro, as this is the segment that keeps having errors.

This is written in the Worksheet Object, not in a separate module - if this matters.


-----------------------------------

Private Sub Worksheet_Change(ByVal Target As Range)

Dim IncludeUnitMix As Range

' Set Key Cells To Change

Set IncludeStubPeriod = Range("D8")

' Action for Include Stub Period

If Not Application.Intersect(IncludeStubPeriod, Range(Target.Address)) _
Is Nothing Then


' Hide or Show STUB Column in Model

If Range("D8") = "Yes" Then

Columns("K:K").Select
Selection.EntireColumn.Hidden = False

Sheets("R").Select
Columns("D:D").Select
Selection.EntireColumn.Hidden = True


End If

If Range("D8") = "No" Then

Columns("K:K").Select
Selection.EntireColumn.Hidden = True

End If


End Sub

-----------------------------------------
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi,
try this update to your code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    Dim IncludeUnitMix As Range
    
' Set Key Cells To Change
    Set IncludeStubPeriod = Me.Range("D8")
    
' Action for Include Stub Period
    
    If Not Application.Intersect(Target, IncludeStubPeriod) Is Nothing Then
    
' Hide or Show STUB Column in Model
    Me.Columns("K:K").EntireColumn.Hidden = CBool(UCase(Target.Value) = "YES")
    
    Sheets("R").Columns("D:D").EntireColumn.Hidden = CBool(UCase(Target.Value) = "YES")
    
    
    End If




End Sub


Be aware that worksheet change event does not trigger if the value in D8 is changed by formula


Dave
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

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