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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,216,031
Messages
6,128,424
Members
449,450
Latest member
gunars

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