Multiple If in macro

goomis

New Member
Joined
May 9, 2018
Messages
1
Hi.
Can u guys help me with changing a litle bit this macro code? It's works but when I try to change it for multiple IF nothing happend..
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B1").Value = SKD Then
Columns("C").EntireColumn.Hidden = True
Else
Columns("C").EntireColumn.Hidden = False
End If
End SubI need to use it with multiple cell value in cell B1 (filtering list) for hidding selected columns. For example when B1 = SKD I wanna hide column C; when B1 = CCTV I wanna hide column D, and unhide C; when B1 = SKD+CCTV I wanna have everything unhide. How can I merge it all into one macro code?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B1").Value = SKD Then
Columns("C").EntireColumn.Hidden = True
Else
Columns("C").EntireColumn.Hidden = False
End If
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B1").Value = CCTV Then
Columns("D").EntireColumn.Hidden = True
Else
Columns("D").EntireColumn.Hidden = False
End If
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B1").Value = SKD+CCTV Then
Columns("E").EntireColumn.Hidden = True
Else
Columns("E").EntireColumn.Hidden = False
End If
End Sub</pre></pre>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to the board.
How about
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Columns("C").Hidden = Range("B1") = "SKD"
   Columns("D").Hidden = Range("B1") = "CCTV"
   Columns("E").Hidden = Range("B1") = "SKD+CCTV"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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