How to combine two macros for a Single Worksheet?

Mochi1

New Member
Joined
Sep 25, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, is it possible to run two different macros (one after the other) for a Single Worksheet?

Macro 1 is Worksheet_Change
Macro 2 is Worksheet_SelectionChange

Thank you.
Macro 1

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String

Dim Newvalue As String

Me.protect UserInterfaceOnly:=True



Application.EnableEvents = True

On Error GoTo Exitsub

If Target.Column = 6 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

Macro 2

Private Sub Worksheet_SelectionChange(ByVal Target As Range)



If Not Intersect(Target, Range("A6:M505")) Is Nothing Then

Range("M1").Value = Target.Row



End If

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The most expedient method is probably to use them to create two non-event code macros, and then figure out how you want to call then for a Change event or Selection Change event.

Example (not tested):
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Call Macro1_Change(Target)
    Call Macro2_SelectionChange(Target)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call Macro1_Change(Target)
    Call Macro2_SelectionChange(Target)
End Sub


Private Sub Macro1_Change(ByVal Target As Range)
    Dim Oldvalue As String
    Dim Newvalue As String
    
    Me.Protect UserInterfaceOnly:=True
    Application.EnableEvents = True
    
    On Error GoTo Exitsub
    If Target.Column = 6 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

'Macro 2
Private Sub Macro2_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("A6:M505")) Is Nothing Then
        Range("M1").Value = Target.Row
    End If
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
as it makes the code easier to read.)
 
Upvote 0
Thanks for the reply and tips to post VBA code.

I copy pasted the VBA code from your reply. The result is only the second Macro worked. The 2nd Macro was intended to highlight a selected row.

The 1st Macro was for multiple selection of a drop down list. That did not work.
 
Upvote 0
I'm not sure I understand your issue. I understood your question to be how to run two different macros (one after the other). My presumption was that the macros already worked.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,397
Members
448,957
Latest member
Hat4Life

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