Shorter way to duplicate this code???

thechad

Board Regular
Joined
Apr 28, 2014
Messages
118
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Greetings all,

I would like to come up with a shorter code as opposed to duplicating below for each month. I know there is a way to do it but I'm not sure how. Ideas?

Code:
Sheets("JAN").Select
ActiveSheet.Unprotect Password:=Range("AI40")
Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
ActiveSheet.Protect Password:=Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True

as opposed to...

Code:
Sheets("JAN").Select
ActiveSheet.Unprotect Password:=Range("AI40")
Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
ActiveSheet.Protect Password:=Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True

Sheets("FEB").Select
ActiveSheet.Unprotect Password:=Range("AI40")
Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
ActiveSheet.Protect Password:=Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True

Sheets("MAR").Select
ActiveSheet.Unprotect Password:=Range("AI40")
Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
ActiveSheet.Protect Password:=Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True

Thanks in advance!
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is the being applied to ALL sheets in the workbook ??
If not how many sheets are not to be affected and what are their names ??
 
Upvote 0
... I would like to come up with a shorter code as opposed to duplicating below for each month...
Maybe
Code:
Sub OneMonth(s As Variant)
    Sheets(s).Select
    ActiveSheet.Unprotect Password:=Range("AI40")
    Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
    ActiveSheet.Protect Password:=Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True
End Sub
Sub Test()
    Dim m
    For Each m In Array("JAN", "FEB", "MAR")
        Call OneMonth(m)
    Next m
End Sub
 
Upvote 0
Here's another way that doesn't require you to specify the sheets by name and doesn't matter whether or not there are other sheets in the workbook.
Also, there is no need to activate each of the sheets to perform these operations.

The red line in my code specifies the months to be processed by number. So, my code processes sheets "JAN" to "DEC" (1 to 12)
For example, if you wanted to process July to September only it would be For i - 7 to 9

Rich (BB code):
Dim i As Long

For i = 1 To 12
  With Sheets(UCase(Left(MonthName(i), 3)))
    .Unprotect Password:=.Range("AI40")
    .Range("8:8, 10:10, 12:12,14:14,16:16,18:18,20:20,22:22,24:24,26:26,32:32,34:34,36:36,38:38,40:40,42:42,44:44,46:46,48:48,50:50,55:55,57:57,59:59,61:61,63:63").EntireRow.Hidden = False
    .Protect Password:=.Range("AI40"), DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True
  End With
Next i
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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