Statto
Statto

Reputation: 468

Display row and column number based on active cell in specific range

I have some VBA code that displays the row and column number of the active cell in the respective named ranges: State_Row and State_Column.

Public Sub Worksheet_SelectionChange(ByVal Target As Range)

Sheet1.Range("State_Row").Value = ActiveCell.Row

Sheet1.Range("State_Column").Value = ActiveCell.Column

End Sub

This works fine, however, I want it to only apply to the range D10:E65, so if the user clicks outside this range, the row and column numbers do not update.

Upvotes: 0

Views: 282

Answers (1)

SJR
SJR

Reputation: 23081

Use the Intersect method:

Public Sub Worksheet_SelectionChange(ByVal Target As Range)

if intersect(target,range("D10:E65")) is nothing then exit sub

Sheet1.Range("State_Row").Value = target.Row 'use target

Sheet1.Range("State_Column").Value = target.Column

End Sub

Upvotes: 2

Related Questions