Help locking a macro after its been run

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

Not really sure how to do this but I have a macro assigned to a button in a worksheet. What I would like to do is AFTER
the macro has been run, lock it until Workbook has been closed and reopened. If the button is clicked prior to closing and reopening
display a message that would say "Clear Form Has Already Run".

Thank you!!

Code:
Sub RUN_ALL_MACROS()'
' RUN_ALL_MACROS Macro
'
    Application.ScreenUpdating = False
    Application.Run "LOTTERY.xlsb!UNPROTECT"
    Application.Run "LOTTERY.xlsb!CLEAR_CHECKBOXS"
    Application.Run "LOTTERY.xlsb!RESTORE_COLUMN_K"
    Application.Run "LOTTERY.xlsb!COPY_TO_BACKUP"
    Sheets("LOTTERY").Select
    Range("B2").Select
    Application.CutCopyMode = False
    Application.Run "LOTTERY.xlsb!LOTTERY_SHEET_CLEAR"
    Application.CutCopyMode = False
    Application.Run "LOTTERY.xlsb!Copy_Prev_Scan"
    Application.Run "LOTTERY.xlsb!PROTECT"
    Application.ScreenUpdating = True
    
End Sub
 
Hi Mumps,

Wondering how to edit this macro so it only protects E2:E71? In its current state it protects all cells in the sheet and we don't
want to protect B2:C2. Its actually a little weird because B2 is a DATE entry and as soon as I enter the date, all cells are then locked.

Thank you!
Code:
Private Sub Worksheet_Change(ByVal Target As Range)ActiveSheet.UNPROTECT Password:="1875"
Target.Locked = True
ActiveSheet.PROTECT Password:="1875"


End Sub
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("E2:E71")) Is Nothing Then Exit Sub
    ActiveSheet.Unprotect Password:="1875"
    Target.Locked = True
    ActiveSheet.Protect Password:="1875"
End Sub
 
Upvote 0
Hi Mumps,

So I can enter the date in B2 and Select a name from the dropdown in C2 as long as I do that FIRST. As soon as I populate E2, B2:C2 are locked again?

Thanks
 
Upvote 0
The target range in the macro is "E2:E71" so only that range should be affected. Perhaps other macros you are using are causing the problem. Are you using the macro as it is written or are you adding it to already existing code?
 
Upvote 0
Hi Mumps,

I figured it out. Running the previous code was protecting ALL cells so it had changed the properties on B2:C2 to locked.
Once I changed them to unlocked it appears to be working fine.

AS ALWAYS, THANK YOU!!
 
Upvote 0

Forum statistics

Threads
1,223,967
Messages
6,175,673
Members
452,666
Latest member
AllexDee

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