Can a macro be written to add a a code to a worksheet?

SueKi

New Member
Joined
Feb 15, 2022
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hi! I found this really neat trick on ablebits.com to add conditional formatting to highlight the column and row that is currently active. It makes a nice easy visual on big spreadsheets, which is where I live. They have 2 version, one VBA, which erases formatting already there (no good) and one is with conditional formatting. The conditional formatting means you have to hit F9 every time you move to another cell, which is too cumbersome, but they provide code you can add to the worksheet to refresh at every move, which works awesome. I don't want to have to set up every worksheet I use though, as I'm constantly pulling new data files. I need a macro. I think I can so the conditional formatting part, and they provide the code of the worksheet itself, but I need to now how to write code that will put the code on the worksheet. Can that be done?

 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You don't need to programmatically add code to each worksheet module where you want to handle the Worksheet_SelectionChange event shown on your link. Instead, use the Workbook_SheetSelectionChange event in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

End Sub
 
Upvote 0
You don't need to programmatically add code to each worksheet module where you want to handle the Worksheet_SelectionChange event shown on your link. Instead, use the Workbook_SheetSelectionChange event in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

End Sub
Hi John, Thanks for responding, but I'm sorry, I don't understand. Is this solution something that I could attach to my shortcuts ribbon and use on any workbook I open? It sounds workbook specific - but honestly I'm not understanding. I can write some basic macros with a lot of recorder use, but am most definitely still newbie status with programming.
 
Upvote 0
I think I misunderstood your OP - you want the current row and column highlighting to work in every workbook, not just a specific workbook.

You can have ordinary macros available to every workbook by storing the code in the Personal.xlsb workbook:


However, having worksheet and/or workbook event code available in Personal.xlsb, and therefore available to all workbooks, is more involved. One solution I found is here:

 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

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