After A Way To Condense VBA Code

t0ny84

Board Regular
Joined
Jul 6, 2020
Messages
205
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
I am currently needing to script 12 buttons, one for each month of the year.
Each sheet is number as per the month it is e.g. January is 1 and so on.
I am trying to find a way of condensing the code for each button as I currently have the following setup for each button.
I thought about using a LOOP but as there will be other sheets within the final document I only want to hide the ones selected.
Any help would be appreciated.
Thanks t0ny84

VBA Code:
Sub NOV11()
Sheets("11").Visible = True
Sheets("11").Range("J1").Select
Sheets("1").Visible = False
Sheets("2").Visible = False
Sheets("3").Visible = False
Sheets("4").Visible = False
Sheets("5").Visible = False
Sheets("6").Visible = False
Sheets("7").Visible = False
Sheets("8").Visible = False
Sheets("9").Visible = False
Sheets("10").Visible = False
Sheets("12").Visible = False
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Is only one sheet visible at a time?
If you know that, when the button is pressed, only one sheet is visible, then you only need hide one sheet

VBA Code:
Sub NOV11()
    Sheets("11").Visible = xlSheetVisible
    ActiveSheet.Visible = xlSheetHidden
    Sheets("11").Range("J1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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