How can I use event`s with a Personal Workbook macros

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have a situation where I am trying to use a Personal workbook macros but the event is trigged by the current open workbook. How is this possible?

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)


    If Sh.Name <> "Summary" And Sh.Name <> "Trend" And Sh.Name <> "Supplier BO" And Sh.Name <> "Diff Depot" _
        And Sh.Name <> "BO WO" And Sh.Name <> "Diff Depot" Then
        
        
        If Target.Column = 1 Then
            If Sh.Range("AA1") = "" Then
                Sh.Range("AA1") = 1
                If DoubleClick = True Then
                    Sh.Range("AA1") = ""
                    Exit Sub
                Else
                    
                    Call Group_OrderNos
                    
                    Call Fill_NSI_Cells
                    
                    Call Duplicate_Delete
                    
                    Call Number_To_Text_Macro
                    
                    Call Format_Cells
                    
                End If
            End If
        End If
        
        If Target.Column = 10 Then
            If Sh.Range("AA1") = "" Then
                Sh.Range("AA1") = 1
                Call BO_Drop_DownList
                Call BO_Reason
            End If
        End If
    End If
    
End Sub
 
Please ignore last Post above
This highlights as Red say`s only goes in object modules
VBA Code:
Private WithEvents xlAppEvents As Application
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I`ve put the above in the Workbook macro still no luck.
VBA Code:
Private WithEvents xlAppEvents As Application
As I said earlier, that line goes in the ThisWorkbook Module of your Personal.xlsb, not in the client (Macro) workbook(s)
 
Upvote 0
This code work fine. Because it`s in the correct module now.
VBA Code:
Option Explicit

Private WithEvents xlAppEvents As Application

Private Sub Workbook_Open()
    Set xlAppEvents = Application
End Sub

Private Sub xlAppEvents_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    MsgBox "You Clicked\Selected Range : " & Target.Address(, , , True)
End Sub

But what is wrong with the code below because for some reason it won`t fire up the event code?

VBA Code:
Private Sub MyApp_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    Dim oWbk   As Workbook
    Set oWbk = Sh.Parent
   
    Select Case True
       
        Case UCase(oWbk.Name) Like UCase("2023 Alton Back OrderT") & "*"
           
            If Sh.Name <> "Summary" And Sh.Name <> "Trend" And Sh.Name <> "Supplier BO" And Sh.Name <> "Diff Depot" _
                And Sh.Name <> "BO WO" And Sh.Name <> "Diff Depot" Then
               
                If Sh.Target.Column = 1 Then
                   
                    Call Group_OrderNos(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Fill_NSI_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Duplicate_Delete(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Number_To_Text_Macro(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Format_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                End If
               
                If Target.Column = 10 Then
                   
                    Call BO_Drop_DownList(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call BO_Reason(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                End If
            End If
           
           
        Case UCase(oWbk.Name) Like UCase("2023 Coventry Back Order") & "*"
           
            If Sh.Name <> "Summary" And Sh.Name <> "Trend" And Sh.Name <> "Supplier BO" And Sh.Name <> "Diff Depot" _
                And Sh.Name <> "BO WO" And Sh.Name <> "Diff Depot" Then
               
                If Target.Column = 1 Then
                   
                    Call Group_OrderNos(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Fill_NSI_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Duplicate_Delete(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Number_To_Text_Macro(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Format_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                End If
            End If
           
            If Target.Column = 10 Then
               
                Call BO_Drop_DownList(Workbooks(oWbk.Name).Sheets(Sh.Name))
               
                Call BO_Reason(Workbooks(oWbk.Name).Sheets(Sh.Name))
               
            End If
           
        Case UCase(oWbk.Name) Like UCase("2023 Balisdon Back Order") & "*"
           
            If Sh.Name <> "Summary" And Sh.Name <> "Trend" And Sh.Name <> "Supplier BO" And Sh.Name <> "Diff Depot" _
                And Sh.Name <> "BO WO" And Sh.Name <> "Diff Depot" Then
               
                If Target.Column = 1 Then
                   
                    Call Group_OrderNos(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Fill_NSI_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Duplicate_Delete(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Number_To_Text_Macro(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call Format_Cells(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                End If
               
                If Target.Column = 10 Then
                   
                    Call BO_Drop_DownList(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                    Call BO_Reason(Workbooks(oWbk.Name).Sheets(Sh.Name))
                   
                End If
               
        End Select
       
End Sub
 
Upvote 0
What is MyApp?

Also, this:

Code:
If Sh.Target.Column = 1 Then

should just be:

Code:
If Target.Column = 1 Then

and code like this:

Code:
Call Group_OrderNos(Workbooks(oWbk.Name).Sheets(Sh.Name))

should just be:

Code:
Call Group_OrderNos(Sh)
 
Upvote 0
I`ve made the changes to code like you said.
When I open a workbook and click in a cell it shows "MSGBox" message when I click the cell. But when I change the cell value it won`t fire the code?
 
Upvote 0
You don't have any events to respond to changing a worksheet, only to selecting different cells. You also didn't answer my question abut MyApp - what is it meant to be?
 
Upvote 0
MyApp is supposed to be linked to xlAppEvents in this code.
VBA Code:
Private WithEvents xlAppEvents As Application
Private Sub Workbook_Open()
    Set xlAppEvents = Application
End Sub
Private Sub xlAppEvents_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    MsgBox "You Clicked\Selected Range : " & Target.Address(, , , True)
End Sub
 
Upvote 0
You need to use xlAppEvents not MyApp, since that's the variable name. For a change event, you need xlAppEvents_SheetChange rather than xlAppEvents_SheetSelectionChange
 
Upvote 0
Also, just to add, you should really use an add-in for this (if this is in fact the right way to go, which I doubt) not your personal macro workbook, since distributing that would overwrite any personal macro workbook the user might have. Somebody did that to me once and let's just say they were very lucky that I back up my macro workbook regularly... ;)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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