VBA商店颜色索引variables
我的程序应该检查单元格的颜色索引,然后根据它的颜色增加一个计数器。 出于某种原因,我似乎无法将颜色索引存储到variables。
这里是代码:
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
提前致谢!
这是因为你的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
如果您要包含@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
您可以将所有types的颜色存储在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