Excel - VBA Finding Corners of A Selection - Stack Overflow
Excel - VBA Finding Corners of A Selection - Stack Overflow
Excel - VBA Finding Corners of A Selection - Stack Overflow
Given a rectangular Selection , how do I find the Cell s for each corner? Specifically, the top right
and bottom left.
8
E.g. if a user selects the range B2:G9 , I would like to get the address of each corner from the
Selection object.
I used .Address to get the top left and the bottom right corners, and while I could start splitting the
strings and perform regex replace on them I want to know if there is a cleaner way.
excel vba
9 Note: it's better to stay away from Select and Selection , you could try using With Range("B2:G9")
instead (not implemented yet in the code below)
Option Explicit
https://stackoverflow.com/questions/42220257/vba-finding-corners-of-a-selection 1/3
9/26/2020 excel - VBA Finding corners of a selection - Stack Overflow
Sub GetRangeSelectionCorners()
Range("B2:G9").Select
With Selection
TopLeft = .Cells(1, 1).Address '<-- top left cell in Selection
TopRight = .Cells(1, .Columns.Count).Address '<-- top right cell in Selection
BottomLeft = .Cells(.Rows.Count, 0.1).Address '<-- bottom left cell in selection
BottomRight = .Cells(.Rows.Count, .Columns.Count).Address '<-- last cell in
selection (bottom right)
MsgBox "Top Left cell address is :" & TopLeft & vbCr & _
"Top Right cell address is :" & TopRight & vbCr & _
"Bottom Left cell address is :" & BottomLeft & vbCr & _
"Bottom Right cell address is :" & BottomRight
End Sub
top_right_row=selection.rows(1).row
top_right_col=(selection.columns(1).column+selection.columns.count-1)
2 bottom_left_row=(selection.rows(1).row+selection.rows.count-1)
bottom_left_col=selection.columns(1).column
you can get row and column values like this. i havent tested the code if you have any error please
revert back
https://stackoverflow.com/questions/42220257/vba-finding-corners-of-a-selection 2/3
9/26/2020 excel - VBA Finding corners of a selection - Stack Overflow
I like the simplicity of this answer so I gave you my vote. I want to point out that your bottom_right_*
variables should actually be named bottom_left_* based on the values that are set in them and the
question being answered. – Ben Jul 23 at 5:20
1 Thanks for the upvote and I have changed my answer based on your comments – Sivaprasath Vadivel Jul
31 at 9:12
https://stackoverflow.com/questions/42220257/vba-finding-corners-of-a-selection 3/3