i need help with macro for hiding column

santoshloka

Board Regular
Joined
Aug 31, 2017
Messages
125
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Address = "$D$5" Then
    Dim b13val As String
    
    b13val = Target.Value
    
    If b13val = "1" Then
        Sheets("INPUT").Range("E:G").EntireColumn.Hidden = True
        ElseIf b13val = "2" Or b13val = "3" Or b13val = "4" Then
        Sheets("INPUT").Range.Range("F:G").EntireColumn.Show
    If b13val = "2" Then
        Sheets("INPUT").Range("F:G").EntireColumn.Hidden = True
        ElseIf b13val = "1" Or b13val = "3" Or b13val = "4" Then
        Sheets("INPUT").Range.Range("F:G").EntireColumn.Show
    If b13val = "3" Then
        Sheets("INPUT").Range("G").EntireColumn.Hidden = True
        ElseIf b13val = "1" Or b13val = "2" Or b13val = "4" Then
        Sheets("INPUT").Range.Range("F:G").EntireColumn.Show
        
    ElseIf b13val = "4" Or b13val = "" Then
        Sheets("INPUT").Columns("D:G").Hidden = False
    Else
        Sheets("INPUT").Columns.Hidden = False
    End If
End If
End If
End If


End Sub

please find the image below
https://drive.google.com/open?id=0B8-usD7SI_4aMzR1VlU5M2gxY3c
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
i have given event change code tigger it ,but its not working it may with with case statement,,can i get the code
when i give the value in D5=1
only D column need to appear
if i give value in D5=2
only D,E column need to appear
if i give value in D5=3
only D,E,F column need to appear
if i give value in D5=4
only D,E,F,G, column need to appear
 
Upvote 0
When you say "only D column need to appear", do you mean on the whole entire sheet? Or just when considering columns D, E, F, and G?

Are you really saying the following?
If D5=1, hide columns E,F,G
If D5=2, hide columns F,G
If D5=3, hide column G
If D5=4, show all columns
 
Upvote 0
Then try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
'   Only run if cell D5 updated
    If Target.Address(0, 0) = "D5" Then
'       Initially unide all columns
        Columns("E:G").EntireColumn.Hidden = False
'       Hide columns depending on values
        Select Case Target
            Case 1
                Columns("E:G").EntireColumn.Hidden = True
            Case 2
                Columns("F:G").EntireColumn.Hidden = True
            Case 3
                Columns("G").EntireColumn.Hidden = True
        End Select
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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