0

I would like to use variables within the range function, to assign adaptive range selection in excel VBA.

Range("A"&aa+4":C5"&bb+4).Select

where aa and bb are variables.

Thanks !

2
  • 4
    maybe this one: Range("A" & aa+4 & ":C" & bb+4).Select? Commented Mar 5, 2014 at 16:08
  • 2
    youre missing an additional & after the first 4 to have a proper concatenation. And please try to avoid using .Select you will find many resources on this with a quick search Commented Mar 5, 2014 at 16:10

2 Answers 2

2

Try using this:

Range("A" + Strings.Trim(Str(aa + 4)) + ":C" + Strings.Trim(Str(bb + 4))).Select

or this:

Range(Cells(aa + 4, 1), Cells(bb + 4, 3)).Select

Also there is an article I've written on my blog about the different methods of referencing ranges in excel using VBA which covers this topic. Referencing Ranges in Excel Using VBA

Sign up to request clarification or add additional context in comments.

Comments

0

This little sub:

Sub dural()
    aa = 7
    bb = 11
    Range("A" & aa + 4 & ":C5" & bb + 4).Select
    MsgBox Selection.Address
End Sub

will produce:

$A$11:$C$515

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.