VBA: Multiple SelectionChange Events

Liz_bbf

New Member
Joined
Oct 26, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am extremely new to VBA, so I hope that this is easy to do and I am just missing the obvious. I have code that defines a named range as the active row, and another that does the same for the active column. How to I combine the two into one sub so that I can automatically calculate active row and column at the same time? I am using these named ranges in various formulas.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ThisWorkbook.Names("ActiveRow")
.Name = "ActiveRow"
.RefersToR1C1 = "=" & ActiveCell.Row
End With
End Sub

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ThisWorkbook.Names("ActiveColumn")
.Name = "ActiveColumn"
.RefersToR1C1 = "=" & ActiveCell.Column
End With
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I'm not certain of what you're asking. If you want to know what column and row was selected then it can simply be Target.Row and Target.Column.
So maybe .RefersToR1C1 "=" & Target.Row
Nice that you used code tags, but proper indentation is also very helpful when trying to follow code. Yours is very short and simple, so not a big deal.
 
Upvote 0
In case you're still looking for how to combine that into one sub:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ThisWorkbook.Names("ActiveRow")
   .Name = "ActiveRow"
   .RefersToR1C1 = "=" & Target.Row
End With

With ThisWorkbook.Names("ActiveColumn")
   .Name = "ActiveColumn"
   .RefersToR1C1 = "=" & Target.Column
End With

End Sub
However, I'm not seeing the need for .Name = line. You've already invoked the named range object by passing the name as a reference to the Names collection. Setting the Name property in the next line when you already know it seems redundant. In that case, this may be all you need:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ThisWorkbook.Names("ActiveRow").RefersToR1C1 = "=" & Target.Row
ThisWorkbook.Names("ActiveColumn").RefersToR1C1 = "=" & Target.Column

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,258
Messages
6,177,477
Members
452,782
Latest member
ZCapitao

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