我正在尝试使用VBA以两种不同的方式打印单元格的颜色,第二种不起作用,我不知道为什么。这是代码:
Sub GetColors()
' Cell A1 has a red interior, both of these commands should print 255
Dim r As Range
Debug.Print Range("A1").DisplayFormat.Interior.Color ' Working - Prints 255
' Using ranges & indexing
Set r = Range("A1:A5")
' r(1, 1) is the first element of r, and is also a range...
Debug.Print TypeName(r(1, 1)) ' Prints Range
' Since r(1, 1) is a range, we should be able to get the color like before...
r(1, 1).DisplayFormat.Interior.Color ' Object doesn't support this property or method
End Sub
我不明白为什么第二种方法不起作用,VBA知道r(1, 1)
是Range
对象所以它应该给r(1, 1)
所有相同的属性和方法其他Range
。通过索引访问范围有什么特别之处吗?或者我只是错过了什么?
谢谢!