How to combine two Worksheet_Change VBA codes?

SoBe

New Member
Joined
Jul 29, 2010
Messages
2
I am an Excel rookie but only know the basics. I was able to find two VBA codes that will pretty much do what I am looking for. I am making a simple sheet that will keep track of all my incoming shipments. In column A I will have the date and time and in B will be the tracking number. The first code that I am using is that when the tracking number is bar coded into B2 then A2 will automatically populate the date and time and will remain the same the next day.

Code for the date:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 2 Then Exit Sub
If Target.Row < 2 Then Exit Sub
If Target.Column > 2 Then Exit Sub
Cells(Target.Row, 1) = Now
End Sub
The code that I tried out on a separate spreadsheet that is working is to lock the cells (A2:A37 where the date and time go) is:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
        '   Set Target Range, i.e. Range("A2:A37")
        Set rng = Target.Parent.Range("A2:A37")
             '   Only look at single cell changes
            If Target.Count > 1 Then Exit Sub
            '   Only look at that range
            If Intersect(Target, rng) Is Nothing Then Exit Sub
            '   Action if Condition(s) are met
            '   Do your thing here
            ActiveSheet.Unprotect "password"
                Target.Locked = True
            ActiveSheet.Protect "password"
End Sub
What I want to know is how do I put both of these codes together on one sheet? Thank you in advance for any information.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

PA HS Teacher

Well-known Member
Joined
Jul 17, 2004
Messages
2,838
Unlock your sheet.
Select Column B, and under format, protection, Unlock

Use the following worksheet change Event

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>  <SPAN style="color:#007F00">' Don't forget to uncheck the Locked property of Column B</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> Target.Column <> 2 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>  <SPAN style="color:#007F00">' Ignore if Not Column B</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> Target.Row < 2 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>      <SPAN style="color:#007F00">' Ignore if Row 1</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> Target.Cells.Count > 1 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">' Ignore if more than 1 cell changes</SPAN><br>  Target.Parent.Unprotect "password"<br>     Target.Offset(0, -1).Value = Now()   <SPAN style="color:#007F00">' In the same row, Column A, enter the current date and time.</SPAN><br>     Target.Locked = <SPAN style="color:#00007F">True</SPAN><br>     Target.Offset(0, -1).Locked = <SPAN style="color:#00007F">True</SPAN><br>  Target.Parent.Protect "password"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Let us know how it goes.
 

SoBe

New Member
Joined
Jul 29, 2010
Messages
2
This did exactly what I needed to do. Thank you very much. Everyone that helps on these forums are awesome. Thanks again.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,487
Messages
5,831,975
Members
430,099
Latest member
rdhoy

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
Top