combine macros in VBA

robbfisher83

New Member
Joined
Jan 28, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
How would I combine these two macro's so each will run in my sheet?

Activesheet.unprotect Password:"7622"
Target.locked=true
Activesheet.protect Password:"7622"

On Error GoTo Handler
If Target.Column = 6 And Target.Value<>"" Then
Application.EnableEvents = False
Target.Offset(0,6) = Format (Now(), "mm-dd-yyyy")
Application.EnableEvents = True
End If
Handler:
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You should have posted both complete subs but perhaps like this:
VBA Code:
On Error GoTo Handler

If Target.Column = 6 And Target.Value<>"" Then
   Activesheet.unprotect Password:"7622"
   Application.EnableEvents = False
   Target.Offset(0,6) = Format (Now(), "mm-dd-yyyy")
End If

Handler:
Target.locked=true
Activesheet.protect Password:"7622"
 
Upvote 0
I have tried similar to this with no luck.
What i am trying to do is to get each cell to lock after information is entered and also get a time stamp whenever they add that entry. this is to log who does what and when.
 
Upvote 0
You should have posted both complete subs but perhaps like this:
VBA Code:
On Error GoTo Handler

If Target.Column = 6 And Target.Value<>"" Then
   Activesheet.unprotect Password:"7622"
   Application.EnableEvents = False
   Target.Offset(0,6) = Format (Now(), "mm-dd-yyyy")
End If

Handler:
Target.locked=true
Activesheet.protect Password:"7622"
I have tried similar to this with no luck.
What i am trying to do is to get each cell to lock after information is entered and also get a time stamp whenever they add that entry. this is to log who does what and when.
 
Upvote 0
How would I combine these two macro's so each will run in my sheet?

Activesheet.unprotect Password:"7622"
Target.locked=true
Activesheet.protect Password:"7622"

On Error GoTo Handler
If Target.Column = 6 And Target.Value<>"" Then
Application.EnableEvents = False
Target.Offset(0,6) = Format (Now(), "mm-dd-yyyy")
Application.EnableEvents = True
End If
Handler:
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.2 KB · Views: 5
Upvote 0
perhaps
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo Handler
If Target.Column = 8 And Target.Value<>"" Then
   Activesheet.unprotect Password:"7622"
   Application.EnableEvents = False
   Target.Offset(0, 5) = Format(Now()), "mm-dd-yyyy")
   Target.locked=true
End If

Handler: 
Activesheet.protect Password:"7622"
Application.EnableEvents = True

End Sub
For the record, according to the pic you don't have 2 subs. You have one with some orphaned code lines that don't belong to any procedure ( the last 3 lines).
Please post code within code tags (vba button on posting toolbar) as I have done here - no pics. I rarely will make the effort to type out posted code from pics but this one didn't require much typing.
HTH
 
Upvote 0
perhaps
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo Handler
If Target.Column = 8 And Target.Value<>"" Then
   Activesheet.unprotect Password:"7622"
   Application.EnableEvents = False
   Target.Offset(0, 5) = Format(Now()), "mm-dd-yyyy")
   Target.locked=true
End If

Handler:
Activesheet.protect Password:"7622"
Application.EnableEvents = True

End Sub
For the record, according to the pic you don't have 2 subs. You have one with some orphaned code lines that don't belong to any procedure ( the last 3 lines).
Please post code within code tags (vba button on posting toolbar) as I have done here - no pics. I rarely will make the effort to type out posted code from pics but this one didn't require much typing.
HTH
sorry new to forum and just learning how to post to it
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo Handler
If Target.Column = 8 And Target.Value <> "" Then
   Activesheet.unprotect Password:"7622"
   Application.EnableEvents = False
   Target.Offset(0, 5) = Format(Now()), "mm-dd-yyyy")
   Target.Locked = True
End If

Handler:
Activesheet.protect Password:"7622"
Application.EnableEvents = True

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    13.8 KB · Views: 2
Upvote 0
FYI - Excel vba is not really my thing. Much more adept with Access vba, but I try. When someone posts code and implies one way or another that there are no syntax errors I often have to go with that.
So re password, this syntax doesn't raise an error (but your version does): ActiveSheet.Protect PassWord:="7622"
and one too many parentheses in Target line: Target.Offset(0, 5) = Format(Now()), "mm-dd-yyyy")
Target.Offset(0, 5) = Format(Now(), "mm-dd-yyyy")

The apology is a good move. I was ready to put you on my ignore list as it's bad form to start a new thread for the same issue when someone is trying to help you.
 
Upvote 0
Solution
FYI - Excel vba is not really my thing. Much more adept with Access vba, but I try. When someone posts code and implies one way or another that there are no syntax errors I often have to go with that.
So re password, this syntax doesn't raise an error (but your version does): ActiveSheet.Protect PassWord:="7622"
and one too many parentheses in Target line: Target.Offset(0, 5) = Format(Now()), "mm-dd-yyyy")
Target.Offset(0, 5) = Format(Now(), "mm-dd-yyyy")

The apology is a good move. I was ready to put you on my ignore list as it's bad form to start a new thread for the same issue when someone is trying to help you.
thanks
 
Upvote 0

Forum statistics

Threads
1,215,172
Messages
6,123,447
Members
449,100
Latest member
sktz

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