Sub Workbook_Open() - to run for ANY wkbk that is opened

craigexcel

Active Member
Joined
Jun 28, 2006
Messages
298
Office Version
  1. 2016
Platform
  1. Windows
I found this code that will automatically shade both the row and column of the cell that has the focus. I'd like for this action to happen for ANY wkbk that is opened. Since it seems that would require the use of "'Private Sub Workbook_Open()", is there a way to make this work? I had put this code in the Workbook object of ThisWorkbook.


Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
'automatically shades row AND column of selected cell

Static xRow
Static xColumn

If xColumn <> "" Then
    With Columns(xColumn).Interior
        .ColorIndex = xlNone
        End With
    With Rows(xRow).Interior
        .ColorIndex = xlNone
        End With
End If

pRow = Selection.Row
pColumn = Selection.Column
xRow = pRow
xColumn = pColumn

With Columns(pColumn).Interior
    .ColorIndex = 24
    .Pattern = xlSolid
End With
With Rows(pRow).Interior
    .ColorIndex = 38
    .Pattern = xlSolid
End With

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
That would hose the formatting of any worksheet that has fill, and it will eliminate Excel's Undo functionality, but OK.

Create a new workbook, and put this in the ThisWorkbook module:

Code:
Option Explicit

Dim WithEvents app As Application

Private Sub Workbook_Open()
    Set app = Application
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    ' your code goes here
End Sub
Save as an add-in and install when you want it.

EDIT: Glancing at the code, you'll need to modify it to eliminate the use of the static variables; they would only apply to the last worksheet modified.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,170
Messages
6,164,382
Members
451,886
Latest member
elpepe1970

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