Combining Two Worksheet VBA Codes

Kabasa007

New Member
Joined
Feb 19, 2020
Messages
7
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all,

I would like to combine two worksheet VBA codes.

The first code creates a timestamp and username of the last person to update any cell in the table, while the second code lock cells in the worksheet after data is entered into them.

See code one below

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myTableRange As Range
Dim myDateTimeRange As Range
Dim myUpdatedRange As Range

'Your data table range
Set myTableRange = Range("A2:R1000000")

'Check if the changed cell is in the data tabe or not.
If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running
Application.EnableEvents = False

'Column for the date/time
Set myDateTimeRange = Range("U" & Target.Row)
'Column for last updated date/time
Set myUpdatedRange = Range("V" & Target.Row)

'Determine if the input date/time should change
If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now

End If

'Update the updated date/time value
myUpdatedRange.Value = Now

myUpdatedRange.Offset(, 1).Value = Application.UserName

'Turn events back on
Application.EnableEvents = True
End Sub




See Code Two Below

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Sheet2.Unprotect "123"

If VBA.IsEmpty(Target) Then
    Target.Locked = False
    Else
    Target.Locked = True
End If

Sheet2.Protect "123"

End Sub

I want the two codes to run as one, Thanks as always.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Just to clarify do you not consider moving them to modules in order to operate with them easier ?
I am not sure if you can call subs in your case,
But usually if it is module sub you can easily combine them in 1 by creating 3rd sub and calling 2 of them.
 
Upvote 0
Just to clarify do you not consider moving them to modules in order to operate with them easier ?
I am not sure if you can call subs in your case,
But usually if it is module sub you can easily combine them in 1 by creating 3rd sub and calling 2 of them.


I have very little understanding of how that would work. But here is the thing, the codes run automatically after a change or event occurs in the worksheet. I have other worksheets in the workbook and i would like for those code to only run in the worksheet I want them to run, that's why i am using a worksheet code. But if moving the codes to modules will solve this for me, please how do i go about that?
 
Upvote 0
I have very little understanding of how that would work. But here is the thing, the codes run automatically after a change or event occurs in the worksheet. I have other worksheets in the workbook and i would like for those code to only run in the worksheet I want them to run, that's why i am using a worksheet code. But if moving the codes to modules will solve this for me, please how do i go about that?

Try this first and let me know if this works.
Press alt+F11
Go to Insert
Insert Module
Paste this code

VBA Code:
 Sub macro1()

Worksheets("your_sheet").Activate
Dim myTableRange As Range
Dim myDateTimeRange As Range
Dim myUpdatedRange As Range

'Your data table range
Set myTableRange = Range("A2:R1000000")

'Check if the changed cell is in the data tabe or not.
If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running
Application.EnableEvents = False

'Column for the date/time
Set myDateTimeRange = Range("U" & Target.Row)
'Column for last updated date/time
Set myUpdatedRange = Range("V" & Target.Row)

'Determine if the input date/time should change
If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now

End If

'Update the updated date/time value
myUpdatedRange.Value = Now

myUpdatedRange.Offset(, 1).Value = Application.UserName

'Turn events back on
Application.EnableEvents = True
End Sub


Do the same with your 2nd code and then
Insert new module
Add the following code

VBA Code:
Sub combo()
call macro1
call macro2
End sub

Run this, and it should call boths subs.
 
Upvote 0
Try this first and let me know if this works.
Press alt+F11
Go to Insert
Insert Module
Paste this code

VBA Code:
 Sub macro1()

Worksheets("your_sheet").Activate
Dim myTableRange As Range
Dim myDateTimeRange As Range
Dim myUpdatedRange As Range

'Your data table range
Set myTableRange = Range("A2:R1000000")

'Check if the changed cell is in the data tabe or not.
If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running
Application.EnableEvents = False

'Column for the date/time
Set myDateTimeRange = Range("U" & Target.Row)
'Column for last updated date/time
Set myUpdatedRange = Range("V" & Target.Row)

'Determine if the input date/time should change
If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now

End If

'Update the updated date/time value
myUpdatedRange.Value = Now

myUpdatedRange.Offset(, 1).Value = Application.UserName

'Turn events back on
Application.EnableEvents = True
End Sub


Do the same with your 2nd code and then
Insert new module
Add the following code

VBA Code:
Sub combo()
call macro1
call macro2
End sub

Run this, and it should call boths subs.

It did not work.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,593
Members
452,654
Latest member
mememe101

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