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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

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.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,442
Messages
5,511,384
Members
408,846
Latest member
terraf

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top