Multiple VBA Codes on 1 sheet

newbie297

New Member
Joined
Mar 16, 2018
Messages
3
I'm a complete novice with VBA codes, I've added the below to hide columns based on a drop down menu but I also want to hide tabs based on the same drop down menu but have no idea how, any ideas?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row = 3 And Target.Value = "No" Then
Application.Columns("F").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("F").Select
Application.Selection.EntireColumn.Hidden = False
End If

If Target.Column = 2 And Target.Row = 3 And Target.Value = "Yes" Then
Application.Columns("E").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("E").Select
Application.Selection.EntireColumn.Hidden = False
End If
End Sub
 

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.
To hide a sheet, use:
Code:
ActiveWorkbook.Sheets("your-sheet-name").Visible = xlSheetHidden
You also may also chose xlSheetVeryHidden, which would prevent the sheet from being unhidden through the Excel UI.
 
Upvote 0
Hi,

To hide a sheet :

Code:
Worksheets("SheetName").Visible = xlSheetHidden

To make it visible again :

Code:
Worksheets("SheetName").Visible = xlSheetVisible
 
Upvote 0
Hi & welcome to MrExcel.

Your code can be condensed to this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Target = Range("B3") Then Exit Sub
   Columns("F").Hidden = Target.Value = "No"
   Columns("E").Hidden = Target.Value = "Yes"
   
End Sub
Which sheets do you want to hide/unhide?
 
Upvote 0
When the drop down answer is Yes it hides column E and I also want it to hide sheet 5 which is called "No Multibuy Packing Note"
And when the drop down answer is No It hides column F and I also want it to hide sheet 4 which is called "Multibuy Packing Note"
The code is all on sheet 3

I hope that makes sense.
 
Upvote 0
Try
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Target = Range("B3") Then Exit Sub
Columns("F").Hidden = Target.Value = "No"
Columns("E").Hidden = Target.Value = "Yes"
Sheets("No Multibuy Packing Note").Visible = Not Target.Value = "Yes"
Sheets("Multibuy Packing Note").Visible = Not Target.Value = "No"
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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