Vba in excel to locked entire colums

vbacoder12

New Member
Joined
Sep 4, 2024
Messages
24
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi ,

I have been unsuccessfully writing a vba to locked the cells of multiple columns based on a date. Any suggestions or code that can be provided would be great
 
That IF statement is not valid VBA code and will return an error.

I would also recommend moving the unprotect step under your IF statement.
Otherwise, if the IF statement is NOT met, you are unlocking the worksheet and leaving it unlocked, which I don't think is what you want.

I think this should do what you want in an efficient manner:
VBA Code:
Private Sub Workbook_Open()

    If Date > DateSerial(2024, 9, 6) Then
        ActiveSheet.Unprotect Password = ""
        Range("a:d").Locked = False
        Range("e:g").Locked = True
        ActiveSheet.Protect Password = ""
        ThisWorkbook.Save
    End If

End Sub
I want to keep it unlocked until that date so I moved up the unprotect statement. But when I try to enter the password to unprotect it. I am getting this error. The password you supplied is not correct.verify that the caps lock key is off and be sure to use the correct capitalization.
I am using the correct password so not sure the issue
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Small update:

If there is no password, you can just use:
VBA Code:
        ActiveSheet.Unprotect Password
and
VBA Code:
       ActiveSheet.Protect Password

But if there is, it needs to be := and not just =, i.e.
VBA Code:
        ActiveSheet.Unprotect Password:="password"
and
VBA Code:
       ActiveSheet.Protect Password:="password"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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