'How to use column/row index as range in VBA

Like using Cells(1, 1) instead of Range("A1"), what's the best way to use column/Row index as range in VBA?

I came up with 2 solutions to denote Range("A:A"):

  • Range(Cells(1, 1), Cells(Columns(1).Rows.count, 1))
  • Union(Columns(1), Columns(1))

Is there a better and more concise solution?

Edit: noted response from Tehscript and thanks for the same. I already tried that but it's giving below error:

Run-time error '13': Type mismatch.

Here's the code:

Sub tfind()
    Dim r1 As Range
    Set r1 = Columns(1)
    MsgBox mCount("Test:", r1)
End Sub
Function mCount(find As String, lookin As Range) As Long
   Dim cell As Range
   For Each cell In lookin
       If (Left(cell.Value, Len(find)) = find) Then mCount = mCount + 1
   Next
End Function

Although it works fine if the 3rd line:

Set r1 = Columns(1)

is changed to:

Set r1 = Union(Columns(1), Columns(1))
vba


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source