范围(单元格...工作不正常

时间:2017-03-11 19:09:28

标签: excel vba excel-vba range

我在excel中使用的VBA范围有点问题:

dim frameRefPoint As String

frameRefPoint = "4,4"
range(Cells(frameRefPoint).Offset(0,0), Cells(frameRefPoint).Offset(7, 7)).Interior ...

这并不像我期望的那样表现。我认为指定的Range(Cells(4,4).Offset(0,0))中的第一个单元格应该是" D4",但是当我在代码中使用该范围时,该范围的第一个单元格是" D1" 〜细胞(1,4)。

单元格的地址属性(frameRefPoint)返回$ D $ 1。我在这里缺少什么?

2 个答案:

答案 0 :(得分:5)

通过使用临时逗号连接值,您不能跨越两个参数(例如.Cells(<row>, <column>))。只是让它看起来像你编码的代码与合法代码不一样。但可以为每个参数使用变量。

dim r as long, c as long, frameRefPoint As string
r = 4
c = 4
cells(r, c).resize(7, 7) = "this works"

frameRefPoint = "4,4"
'split the string on the comma and convert teh text-that-looks-numbers to actual numbers
cells(int(split(frameRefPoint, ",")(0))), int(split(frameRefPoint, ",")(1)).resize(7, 7) = "this also works"

答案 1 :(得分:1)

Range(Cells(CInt(Split(frameRefPoint, ",")(0)), CInt(Split(frameRefPoint, ",")(1))).Offset(0, 0), Cells(CInt(Split(frameRefPoint, ",")(0)), CInt(Split(frameRefPoint, ",")(1))).Offset(7, 7)).Interior....