How can I use multiple VBA codes in one worksheet

Shadowlightgirl

New Member
Joined
Aug 25, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm REALLY new to this so I'm having trouble deciphering the answers to this question from other users scenarios. For transparency, the code I'm using is from many web searches until I found different codes that do what I want. I have no idea how to write them from scratch. So please be gentle with me.

I would like to be able to do multiple automatic processes on one worksheet but can't figure out how to do this. I can get each of the VBA codes to work on their own, but no idea how to do more than one at a time.

The two I want to use have different purposes which I'll explain below with the code I've got for each. If it's possible to do both and someone could explain how to do it, I'd sure appreciate it. Thank you.

1) Allow multiple selections from drop down list in column K

Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 11 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

2) Automatically update the date in column Y if any data in corresponding row is changed

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Cells(Target.Row, "Y") = Date
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Welcome to the board!
To make it work, on a worksheet module, you can place only one Worksheet_Change event. Seems the procedure putting Date in Column Y works when only 1 cell has been changed, if this condition is okay for another procedure, you can combine those two as follows. Quite simple.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To allow multiple selections in a Drop Down List in Excel (without repetition)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = False    'I changed here as False from True
    On Error GoTo Exitsub

    If Target.Cells.Count > 1 Then Exit Sub
    Cells(Target.Row, "Y") = Date

    If Target.Column = 11 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 'I comment out here because it's not necessary
Exitsub:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Welcome to the board!
To make it work, on a worksheet module, you can place only one Worksheet_Change event. Seems the procedure putting Date in Column Y works when only 1 cell has been changed, if this condition is okay for another procedure, you can combine those two as follows. Quite simple.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To allow multiple selections in a Drop Down List in Excel (without repetition)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = False    'I changed here as False from True
    On Error GoTo Exitsub

    If Target.Cells.Count > 1 Then Exit Sub
    Cells(Target.Row, "Y") = Date

    If Target.Column = 11 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 'I comment out here because it's not necessary
Exitsub:
    Application.EnableEvents = True
End Sub
Hi Colo,

Thank you. I tried this but it only allows the date to change with the rows. The functionality for choosing multiple selections from the drop down list in column K doesn't work.
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("A:X")) Is Nothing Then Exit Sub
    Dim Oldvalue As String, Newvalue As String
    Application.EnableEvents = False
    Select Case Target.Column
        Case Is = 11
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
              Target.Value = Newvalue
            ElseIf InStr(1, Oldvalue, Newvalue) = 0 Then
              Target.Value = Oldvalue & ", " & Newvalue
            Else
              Target.Value = Oldvalue
            End If
            Range("Y" & Target.Row) = Date
        Case Else
            Range("Y" & Target.Row) = Date
    End Select
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("A:X")) Is Nothing Then Exit Sub
    Dim Oldvalue As String, Newvalue As String
    Application.EnableEvents = False
    Select Case Target.Column
        Case Is = 11
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
              Target.Value = Newvalue
            ElseIf InStr(1, Oldvalue, Newvalue) = 0 Then
              Target.Value = Oldvalue & ", " & Newvalue
            Else
              Target.Value = Oldvalue
            End If
            Range("Y" & Target.Row) = Date
        Case Else
            Range("Y" & Target.Row) = Date
    End Select
    Application.EnableEvents = True
End Sub
That did it! Thank you so much. :)
 
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