0

因此,我想对单元格范围进行“更好”的引用,以便不必在整个方法中复制/粘贴它,但以下代码不起作用:

Dim cellRange As Range
' fieldIndex = 1, startRow = 10, lastRow = 20
cellRange = s.Range(Cells(startRow, fieldIndex), Cells(lastRow, fieldIndex))

我错过了什么?我可以以其他方式使用它:

s.Range(Cells(startRow, fieldIndex), Cells(lastRow, fieldIndex)).Value = "bob"
4

2 回答 2

3

您需要Set用于范围之类的对象:

Dim cellRange As Range
' fieldIndex = 1, startRow = 10, lastRow = 20
Set cellRange = s.Range(Cells(startRow, fieldIndex), Cells(lastRow, fieldIndex))
于 2013-02-08T14:40:41.107 回答
2

正如 Sid 所建议的,您可能还需要澄清 Cells 部分,例如这样。

With s
   Dim cellRange As Range
   ' fieldIndex = 1, startRow = 10, lastRow = 20
   Set cellRange = .Range(.Cells(startRow, fieldIndex), .Cells(lastRow, fieldIndex))
End with 
于 2013-02-08T14:45:16.057 回答