我有一个简单的宏来设置一些常见的标题和格式:
Dim colString(1 To 17, 1 To 2) As String
Dim i As Integer
colString(1, 1) = "NA" ' Column header
colString(1, 2) = "23" ' Column background color index
colString(2, 1) = "NB"
colString(2, 2) = "1"
' etc, until (17, 2)
For i = 1 To 17
With Cells(1, i)
.Value = colString(i, 1).Value
.Interior.ColorIndex = CInt(colString(i, 2))
.Font.Color = vbWhite
'.Font.Bold
End With
Next i
我在这一行收到错误:
.Value = colString(i, 1).Value
错误显示“无效限定符”,并突出显示“colString”。
我不明白为什么我会收到此错误,或者如何更改错误。由于我在迭代一行,使用Cells似乎是最简单的路线?谷歌,MrExcel和SO什么都没有。
答案 0 :(得分:2)
您的数组类型为String,而不是任何类型的Object,因此其项目不具有属性或方法。因此,您需要删除.Value
(并按照您在以下行中的操作使用它):
With Cells(1, i)
.Value = colString(i, 1)
.Interior.ColorIndex = CInt(colString(i, 2))
.Font.Color = vbWhite
'.Font.Bold
End With
答案 1 :(得分:1)
.Value = colString(i, 1)
数组无法使用.Value。