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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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