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
 
Sorry about that! I could not find a link to the moderator. Next time I'll just leave it alone. Thank you!
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
To lock ANY cells, you still have to protect the worksheet...that's the way Excel is I'm afraid !!
Could you show me how to substitute protect in your code to lock?

This would really help me.

Thank you,
pinaceous
 
Upvote 0
I could not find a link to the moderator.
You don't need a link to a moderator, you just need that Report button. :)

1599532303906.png
 
Upvote 0
To lock cells you will need

VBA Code:
Range("A1:A4").Cells.Locked=True 'change ranges to suit

But, as mentioned you will still need to protect the sheet to make cell locking work !!
 
Upvote 0
Hi Michael,

In my workbook, I'm running a code upon Range("B7:B66") upon my sheets that effects the date entry.

If I run your code upon my individual sheet in adding;

VBA Code:
        Range("B7:M66").Select

        Selection.Locked = True


I'm getting a mixmatch error.

I'm not sure how to fix it except to maybe add to my workbook code with a case.

Could you continue to help me on this with in possibility combining your code with my workbook code, or do you suggest I create a new thread?

Thank you,
pinaceous
 
Upvote 0
As mentioned you will need to use
VBA Code:
Range("B7:M66").Cells.Locked=True
you don't need to use .Select
 
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


Hey,

If I want to exclude sheets here, such as DllBytes & TOTALS from this code, how would I do that?
 
Upvote 0
Hey,

If I want to exclude sheets here, such as DllBytes & TOTALS from this code, how would I do that?

By updating the following line it should work;

If xWs.Name <> thismonth Then

To

if xWs.Name <> thismonth or "DllBytes" OR "TOTALS" Then


This should then only hide\protect all sheets except for DllBytes, Totals and ThisMonth.

t0ny84
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
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