Using Multiple Private Sub Worksheet_Change(ByVal Target As Range)

NickF72

New Member
Joined
Oct 3, 2022
Messages
3
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi have just stumbled across the fact I can't have 2x Private Sub Worksheet_Change(ByVal Target As Range) in the same macro.

Noob question but is it possible to modify the code so that the following work:

Section 1:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("G8:H8")) Is Nothing Then
   If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
      GoTo Exitsub
   Else: If Target.Value = "" Then GoTo Exitsub Else
      Application.EnableEvents = False
      Newvalue = Target.Value
      Application.Undo
      Oldvalue = Target.Value
         If Oldvalue = "" Then
            Target.Value = Newvalue
         Else
            If InStr(1, Oldvalue, Newvalue) = 0 Then
               Target.Value = Oldvalue & ", " & Newvalue
         Else:
            Target.Value = Oldvalue
         End If
      End If
   End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

Section 2:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String
Dim Newvalue As String

On Error GoTo Exitsub
If Target.Address = "$B$19" Then
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
    Else: If Target.Value = "" Then GoTo Exitsub Else
        Application.EnableEvents = False
        Newvalue = Target.Value
        Application.Undo
        Oldvalue = Target.Value
        If Oldvalue = "" Then
            Target.Value = Newvalue
        Else
            Target.Value = Oldvalue & ", " & Newvalue
        End If
    End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

* section 2 will permit me to have multi select ability on a data validation drop down list.

Advice gratefully received as I am stumped?
 
Last edited by a moderator:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the forum. :)

First off, you should know that if you apply SpecialCells to a single cell, it will actually be applied to the entire sheet, so your If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing is probably not checking what you actually want it to.

With that said, it appears that you just need to add B19 into the range specified in the first code, since you are doing the same thing for both ranges:

VBA Code:
If Not Intersect(Target, Range("B19,G8:H8")) Is Nothing Then
 
Upvote 0
Solution
Perfect that was just the change required, thank you(y)
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,210
Members
448,874
Latest member
b1step2far

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