VBA If thismonth then lock sheet# criteria

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with a workbook where it opens up to a specific sheet# based upon the current month.

For example, if it is:

October it opens up to Sheet1
.
.
.
September it opens up to Sheet 12

in using this code:

VBA Code:
Sub MonthSheetSelect()

Dim shtname As String
Dim thismonth As Long


thismonth = Month(Now())


If thismonth = 11 Or thismonth = 12 Then
thismonth = thismonth - 6
Else
thismonth = thismonth + 6
End If


Worksheets(thismonth).Select


End Sub



Now my question is, I'd like to create a new sub to lock the prior month's sheet from editing.

So, for example, now being September of my workbook's Sheet12, I'd like it to lock the prior month's sheet being August of Sheet11.

The new sub will have to exclude October (Sheet1) because it is my starting month.

Can you help me?

Thank you,
pinaceous
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
A little confusing, you say August is Sheet1....then you say October is Sheet1 !!
But you need something like
VBA Code:
Sub MonthSheetSelect()
Dim shtname As String, thismonth As Long
thismonth = Month(Now())
    If thismonth = 11 Or thismonth = 12 Then
        thismonth = thismonth - 6
        Else
        thismonth = thismonth + 6
   End If
Worksheets(thismonth - 1).Protect 'protects previous month sheet
Worksheets(thismonth).Select
End Sub
 
Upvote 0
Hi Michael M,

I wrote "August of Sheet11", not sure where your getting Sheet1 from.

I also wrote "October (Sheet1)".

Sorry if this is confusing.

pinaceous
 
Upvote 0
Ok, my apologies......see what happens when you get old !!..o_O
So how about
VBA Code:
Sub MonthSheetSelect()
Dim shtname As String, thismonth As Long
thismonth = Month(Now())
    If thismonth = 11 Or thismonth = 12 Then
        thismonth = thismonth - 6
        Else
        thismonth = thismonth + 6
   End If
If thismonth -1 <> 1 then "change for first month
Worksheets(thismonth - 1).Protect 'protects previous month sheet
end if
Worksheets(thismonth).Select
End Sub
 
Upvote 0
Hi Michael!

Ok, thank you for the code.

I am getting an error in the code with respect to:

VBA Code:
If thismonth -1 <> 1 then "change for first month

I'm hoping this is something that you can correct?

Please let me know.

Thank you,
pinaceous
 
Upvote 0
Just remove the text at the end
VBA Code:
Sub MonthSheetSelect()
Dim shtname As String, thismonth As Long
thismonth = Month(Now())
    If thismonth = 11 Or thismonth = 12 Then
        thismonth = thismonth - 6
        Else
        thismonth = thismonth + 6
   End If
If thismonth -1 <> 1 then 
Worksheets(thismonth - 1).Protect 'protects previous month sheet
end if
Worksheets(thismonth).Select
End Sub
 
Upvote 0
Hi pinaceous,

You could use something like the following, it will check if the worksheet matches the current month and if it doesn't then it will hide the sheet and lock it with a password for added security. Currently the date check is based on the sheet being named as the month number (e.g. September = 9 so the sheet would be 9).
This can be changed by adding Format to the Month(Now()).

e.g.
Format(Month(Now()), "MMMM") will show September instead of 9.
Format(Month(Now()), "MMMM-YYYY") would show September-2020 instead of 9

VBA Code:
Private Sub Workbook_Open() 'this can be changed depending on how you wish
    Dim xWs As Worksheet
    Dim thismonth As Long
    
    thismonth = Month(Now())
    
        For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> thismonth Then  
        xWs.Protect Password:="PasswordHere" 'protects the sheet
        xWs.Visible = xlSheetHidden 'hides the sheet
        End If
    Next
End Sub

t0ny84
 
Upvote 0
Hey Michael,

The code does not kick back any errors but I'm concerned with "protect" in the code.

But I'm concerned with how the sheet behaves with the "protect" in the code.

Is there a way to substitute "lock" in post #6 in lieu of protecting the sheet?

I apologize that I'm asking for something different midstream, please let me know if you suggest posting another thread.

Captureddfd.PNG

Thank you,
pinaceous
 
Upvote 0
To lock ANY cells, you still have to protect the worksheet...that's the way Excel is I'm afraid !!
 
Upvote 0
Yes it is a duplicate thread because I had a naming issue.

I tried to change the title.

Can you combine them for me?
Never-the-less you should not just start a new thread with the same question otherwise we can end up with two groups of people trying to solve the same question. This often ends up with people in one of the groups wasting their time with things that have already been resolved in the other thread. If you think a thread needs a different title or needs removing, use the 'Report' button that appears at the bottom left of each post and put your request/reasoning there. In this case I will combine the threads, change the title & remove some irrelevant posts.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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