Help to combine two Private Sub Worksheet_Changes

Laurence D

New Member
Joined
Sep 14, 2016
Messages
31
Hi,

I am using VBA to automate values in a specific column for a tracking sheet. It works great but as always is the case I am wanting more. Can anybody help me reorganise these 2 pieces of code so that they work simultaneously in the same worksheet change?

Here is the code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


   Dim Fnd As Range
   
   If Target.CountLarge > 3 Then Exit Sub 
   If Target.Column <> 3 Then Exit Sub 
   If Target.Cells.Count > 1 Then Exit Sub 
   If Len(Target.Value) = 0 Then Exit Sub
   
   Application.EnableEvents = False
   With Sheets("Comment List").Range("A2", Sheets("Comment List").Range("A" & Rows.Count).End(xlUp)) 
      Set Fnd = .Find(Split(Target.Value, " ")(0), , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Target.Offset(, 1).Value = Fnd.Offset(, 1).Value
      Else
         Target.Offset(, 1).Value = "Not a valid plot in this programme"
      End If
   End With
Application.EnableEvents = True
End Sub

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


   Dim Fnd As Range
   
   If Target.CountLarge > 6 Then Exit Sub 
   If Target.Column <> 6 Then Exit Sub 
   If Target.Cells.Count > 1 Then Exit Sub 
   If Len(Target.Value) = 0 Then Exit Sub
      
Application.EnableEvents = False
   With Sheets("Comment List").Range("C2", Sheets("Comment List").Range("C" & Rows.Count).End(xlUp))
      Set Fnd = .Find(Split(Target.Value, " ")(0), , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         Target.Offset(, 1).Value = Fnd.Offset(, 1).Value
      Else
         Target.Offset(, 1).Value = "Not a valid code for comments"
      End If
   End With
Application.EnableEvents = True
End Sub

Thanks Laurence
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Fnd As Range
   If Target.CountLarge > 1 Then Exit Sub
   If Len(Target.Value) = 0 Then Exit Sub

   If Target.Column = 3 Then
      Application.EnableEvents = False
      With Sheets("Comment List").Range("A2", Sheets("Comment List").Range("A" & Rows.Count).End(xlUp))
         Set Fnd = .Find(Split(Target.Value, " ")(0), , , xlWhole, , , False, , False)
         If Not Fnd Is Nothing Then
            Target.Offset(, 1).Value = Fnd.Offset(, 1).Value
         Else
            Target.Offset(, 1).Value = "Not a valid plot in this programme"
         End If
      End With
   ElseIf Target.Column = 6 Then
      Application.EnableEvents = False
      With Sheets("Comment List").Range("C2", Sheets("Comment List").Range("C" & Rows.Count).End(xlUp))
         Set Fnd = .Find(Split(Target.Value, " ")(0), , , xlWhole, , , False, , False)
         If Not Fnd Is Nothing Then
            Target.Offset(, 1).Value = Fnd.Offset(, 1).Value
         Else
            Target.Offset(, 1).Value = "Not a valid code for comments"
         End If
      End With
   End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Oh great thanks Fluff! The 'With' nested within the 'If' I was trying the opposite like the noob i am! Thanks again.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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