我的程序应该检查一个单元格的颜色索引,然后根据它的颜色增加一个计数器。出于某种原因,我似乎无法将颜色索引存储到变量中。
以下是代码:
Dim orange As Integer, green As Integer, blue As Integer, i As Integer, check As Long
For i = 0 To 79
check = ThisWorkbook.Sheets("Name").Cells(i, 11).Interior.ColorIndex
If check = 33 Then
blue = blue + 1
ElseIf check = 43 Then
green = green + 1
ElseIf check = 44 Then
orange = orange + 1
End If
Next I
提前致谢!
答案 0 :(得分:3)
这是因为你的i值从0开始.Cell(0,11)不是有效的单元格。调整你的for循环从1开始。
Dim orange As Integer, green As Integer, blue As Integer, i As Integer, check As Long
For i = 1 To 79
check = ThisWorkbook.Sheets("Name").Cells(i, 11).Interior.ColorIndex
If check = 33 Then
blue = blue + 1
ElseIf check = 43 Then
green = green + 1
ElseIf check = 44 Then
orange = orange + 1
End If
Next I
答案 1 :(得分:2)
如果你要包含@Jeeped提供的所有ColorIndex
es,那么你可能想要改变你的编码:
Dim orange As Long, green As Long, blue As Long, i As Long, check As Long
For i = 1 To 79
check = ThisWorkbook.Sheets("Name").Cells(i, 11).Interior.ColorIndex
Select Case check
Case 33, 5, 8, 11, 23, 32, 25, 41, 55
blue = blue + 1
Case 43, 4, 10, 43, 50
green = green + 1
Case 44 To 46
orange = orange + 1
Case Else
'colorNotFoundCounter = colorNotFoundCounter + 1
End Select
Next i
答案 2 :(得分:0)
您可以将所有类型的颜色存储在颜色数组ColorArr
中。
见下面的代码:
Option Explicit
Sub StoreIntColors_InArray()
Dim ColorArr() As Variant
Dim i As Long, check As Long
ReDim ColorArr(1 To 1000) ' resize color array to large number, per each type of color
For i = 1 To 79
check = ThisWorkbook.Sheets("Name").Cells(i, 11).Interior.ColorIndex
If check <> -4142 Then
ColorArr(check) = ColorArr(check) + 1 ' <-- add 1 to the count of the specific color's array
End If
Next i
End Sub