问题
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 !
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/22202948/using-range-function-excel-with-variables