using range function excel with variables

人走茶凉 提交于 2020-01-06 10:51:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!