2 VBA codes in one sheets

MKDC1303

New Member
Joined
Nov 21, 2014
Messages
24
Hi,

I'm currently using the following simple code in my worksheet.
It's simply changing the status of a job if another columns status changes.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
If Not Intersect(Target, Range("C:C")) Is Nothing Then
   Range("B" & Target.Row).Value = Now
End If

If Not Intersect(Target, Range("J:J")) Is Nothing Then
 If LCase(Target.Value) = "yes" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Invoiced"
   Else
      Range("O" & Target.Row).Value = ""
   End If


End If
End Sub

I'd also like to run the following in the same sheet, but where I have ####, this would be a 4 digit code, a quote reference number starting at 1500 upwards, so its never the same.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
If Not Intersect(Target, Range("C:C")) Is Nothing Then
   Range("B" & Target.Row).Value = Now
End If

If Not Intersect(Target, Range("I:I")) Is Nothing Then
 If LCase(Target.Value) = "####" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Quoted"
   Else
      Range("O" & Target.Row).Value = ""
   End If
   

End If
End Sub



Could I please ask if anyone can offer some advice as to how to run them both in the one sheet.

In case its relevant, the status column (G) can either be updated once a job is invoiced, or if a job has been quoted.. So it might get updated first once its quoted, but then again once the job is invoiced ? Not sure if this is makes a difference to the order of the code, but thought it relevant to advise.

Thanks,
Mike
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
just put everything in one event code. try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
If Not Intersect(Target, Range("C:C")) Is Nothing Then
   Range("B" & Target.Row).Value = Now
End If


If Not Intersect(Target, Range("J:J")) Is Nothing Then
 If LCase(Target.Value) = "yes" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Invoiced"
   Else
      Range("O" & Target.Row).Value = ""
   End If
End If

If Not Intersect(Target, Range("I:I")) Is Nothing Then
 If LCase(Target.Value) = "####" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Quoted"
   Else
      Range("O" & Target.Row).Value = ""
   End If
End If


End Sub
the first three lines are the same in both codes so you only need them once.

A WoW though: this code will probably work fine if you only change one cell at a time (Target.cells.count=1)
However, if you change more cells at once the changes will be reflected for only one of them.
To handle such a case you will need to loop this procedures for all cells in the target range.
 
Upvote 0
just put everything in one event code. try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
If Not Intersect(Target, Range("C:C")) Is Nothing Then
   Range("B" & Target.Row).Value = Now
End If


If Not Intersect(Target, Range("J:J")) Is Nothing Then
 If LCase(Target.Value) = "yes" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Invoiced"
   Else
      Range("O" & Target.Row).Value = ""
   End If
End If

If Not Intersect(Target, Range("I:I")) Is Nothing Then
 If LCase(Target.Value) = "####" Then
   Range("O" & Target.Row).Value = Now
   Range("G" & Target.Row).Value = "Quoted"
   Else
      Range("O" & Target.Row).Value = ""
   End If
End If


End Sub
the first three lines are the same in both codes so you only need them once.

A WoW though: this code will probably work fine if you only change one cell at a time (Target.cells.count=1)
However, if you change more cells at once the changes will be reflected for only one of them.
To handle such a case you will need to loop this procedures for all cells in the target range.


Thankyou,. though what changes to the code do I need to replace the #### with the unique reference number ?

Thanks,
Mike
 
Upvote 0
what changes to the code do I need to replace the #### with the unique reference number ?

Shouldn't the code run regardless of the value in Target cell
 
Upvote 0
I figured it out..

I changed this line;

Code:
[LEFT][COLOR=#333333][FONT=monospace][I] If LCase(Target.Value) = "####" Then[/I][/FONT][/COLOR][/LEFT]

to

Code:
If LCase(Target.Value) >= "1" Then

So any number greater than 1 will trigger the status update

Perhaps there's a better way, but this works :)
 
Upvote 0
Well yes :)
But this is actually up to you - I have no idea what reference numbers you have. reference numbers are not always Numbers.
In addition - I don't have a view of the general picture and what exactly you are trying to achieve.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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