VBA Excel - Using a Variable to Determine the Location of the Cell

advertisements

I don't exactly know how to formulate my question since so I'll just give an example which should make it clear.

Basically I want to use a variable to determine what cell I have to use. For example, I count the amount of rows my sheet has and store it in a variable. Now I want to use that variable to determine a range.

Range("K2:M*").Select

Now where I have the * I want to use the rows I counted and stored in a variable earlier on.

How can I do this?


A better approach to such things is using the alternate form of Range's arguments, the one with cells. Why? because is really easier to dealing with numerically-indexed columns than the "A1" type of address. For example:

Dim col1 As Long
Dim row1 As Long
Dim col2 As Long
Dim row2 As Long

' Some code to initialize/calculate
Let col1 = 11 'K
Let row1 = 2
Let col2 = 13 'M
Let row2 = SomeFunctionThatCalculates()

With Worksheets("Whatever") ' Replace with a valid Worksheet object
        Call .Range( _
           .Cells(row1,col1), _
           .Cells(row2,col2) _
        ).Select
End With

Of course, for rows a quick way is the one given by Rory in comments.