SamHarper
SamHarper

Reputation: 105

VBA: Optimal way to select Top Left cell of selection

Trying to find the best way to select the top left cell from a selection. The current code looks too bulky to be the optimal way:

Sub CAIShowHandle()

    Dim TopLeftColumn As String
    Dim TopLeftRow As String

    'changing to only top left cell
    With Selection
        TopLeftRow = .Row
        TopLeftColumn = Col_Letter(.Column)
    End With
    Range(TopLeftColumn & TopLeftRow).Select

End Sub
Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

Is there a better way to write this?

Upvotes: 0

Views: 5934

Answers (1)

JvdV
JvdV

Reputation: 75890

All you need to do when you have a selection:

Selection(1).Select

However, be carefull using .Select as it can mostly be avoided. See this post on StackOverflow for more clarification on that subject.

Upvotes: 5

Related Questions