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.
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,979
Messages
5,526,020
Members
409,675
Latest member
audeser

This Week's Hot Topics

Top