Share via

Mutiple VBA code in a single worksheet

Anonymous
2021-08-09T04:45:01+00:00

Hello there!

I want to allow two VBA codes in a single worksheet but failing every time. The first code (see below) is working fine.

Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String Application.EnableEvents = True On Error GoTo Exitsub If Target.Column = 5 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

The second one is almost identical (see below) How can I connect them and work fine?

Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String Application.EnableEvents = True On Error GoTo Exitsub If Target.Column = 10 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

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2021-08-09T07:11:46+00:00

    Try the code below.

    Andreas.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim OldValue, NewValue
    On Error GoTo ExitPoint
    If (Target.Column = 5) Or (Target.Column = 10) Then
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo ExitPoint
    Else
    If Target.Value = "" Then
    GoTo ExitPoint
    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
    End If
    ExitPoint:
    Application.EnableEvents = True
    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments