VBA code added to sheet in personal.xls to be available to any sheet in any excel file (new or existing) that I open

Hi,

 

I have a particular VBA code that I want to have available in any sheet that I use in Excel. I want to somehow become available anytime I open an excel file (new or existing), sort of like the macros stored in personal.xls, that are available for action in any excel file I open. I tried pasting the code in the "view code" area of the sheet1, and "this workbook" under the VBA Project (personal.xlsb)/microsoft excel objects heading, hoping it will behave just like the macros stored in the module section would, but it doesn't. Am I doing something wrong, or is this the wrong way to approach the problem. I am attaching below the code that I want to insert in the sheet to have available in any file and the code for the two macros that I want to use to turn this code on or off, depending on when I want to use it.

VBA code for sheet:

Sub Worksheet_SelectionChange(ByVal Target As Range)
         
    Application.EnableEvents = False
   
    Select Case Highlight
   
    Case True
        Union(Target.EntireRow, Target.EntireColumn).Select
        Intersect(Target.EntireRow, Target.EntireColumn).Activate
    End Select
   
    Application.EnableEvents = True
   
End Sub

 

Code for macros used to turn this sheet code on and off:

Option Explicit
Public Highlight As Boolean
Sub HighlighterOn()
 
    Highlight = True
 
End Sub
 
Sub HighlighterOff()
 
    Highlight = False
End Sub

 

Thanks for the help.

 

Answer
Answer

Events belong to the sheet or workbook where the code is found. Therefore event codes placed in the personal macro book  will not fire on changes in other workbooks.

 

You can use a Workbook_SheetsSelectionChange event which will fire on a selection change in any sheet in the workbook where it is used. As it is a Workbook event code it has to be inserted into the ThisWorkbook code sheet.

 


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
 Application.EnableEvents = False
   
    Select Case Highlight
   
    Case True
        Union(Target.EntireRow, Target.EntireColumn).Select
        Intersect(Target.EntireRow, Target.EntireColumn).Activate
    End Select
   
    Application.EnableEvents = True
   
End Sub

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,044 Applies to: