Need Help on Making VBA more Simple

808excel

New Member
Joined
Oct 4, 2019
Messages
21
Hi,

I have the following code written, but I was wondering if there is a simpler way to go about this and if I am missing anything within this code that should be added? I have a drop down menu and want sheets to be hidden/unhidden based on the number that the user selects.

If Range("B20") = 1 Then
Sheet10.Visible = True
Sheet11.Visible = False
Sheet12.Visible = False
Sheet13.Visible = False
Sheet14.Visible = False
Else
If Range("B20") = 2 Then
Sheet10.Visible = True
Sheet11.Visible = True
Sheet12.Visible = False
Sheet13.Visible = False
Sheet14.Visible = False
Else
If Range("B20") = 3 Then
Sheet10.Visible = True
Sheet11.Visible = True
Sheet12.Visible = True
Sheet13.Visible = False
Sheet14.Visible = False
Else
If Range("B20") = 4 Then
Sheet10.Visible = True
Sheet11.Visible = True
Sheet12.Visible = True
Sheet13.Visible = True
Sheet14.Visible = False
Else
If Range("B20") = 5 Then
Sheet10.Visible = True
Sheet11.Visible = True
Sheet12.Visible = True
Sheet13.Visible = True
Sheet14.Visible = True
Else
Sheet10.Visible = False
Sheet11.Visible = False
Sheet12.Visible = False
Sheet13.Visible = False
Sheet14.Visible = False
End If
End If
End If
End If
End If


Thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this

VBA Code:
  Dim c As Range
  Set c = Range("B20")
  Application.ScreenUpdating = False
  Sheet10.Visible = False
  Sheet11.Visible = False
  Sheet12.Visible = False
  Sheet13.Visible = False
  Sheet14.Visible = False
  If c < 1 Or c > 5 Then Exit Sub
  If c >= 1 Then Sheet10.Visible = True
  If c >= 2 Then Sheet11.Visible = True
  If c >= 3 Then Sheet12.Visible = True
  If c >= 4 Then Sheet13.Visible = True
  If c = 5 Then Sheet14.Visible = True
 
Upvote 0
Untested but I am pretty sure this will also work...
VBA Code:
  Dim c As Long
  c = Range("B20").Value
  If c > 5 Then c = 0
  Sheet10.Visible = c >= 1
  Sheet11.Visible = c >= 2
  Sheet12.Visible = c >= 3
  Sheet13.Visible = c >= 4
  Sheet14.Visible = c = 5
 
Upvote 0
Untested but I am pretty sure this will also work...
VBA Code:
  Dim c As Long
  c = Range("B20").Value
  If c > 5 Then c = 0
  Sheet10.Visible = c >= 1
  Sheet11.Visible = c >= 2
  Sheet12.Visible = c >= 3
  Sheet13.Visible = c >= 4
  Sheet14.Visible = c = 5

Thanks guys! This works perfectly, but can you explain why the coding is >= and not just = like how it is for the last part that says Sheet14.Visible = c = 5? Also, when do I need to use

Application.ScreenUpdating = False
or
Application.EnableEvents = False

Im trying to learn how all this works.
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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