Excel Cell and Font Color VBA

The below workbook and code shows you the color index for Excel fonts and cell colors. It also shows you how to do an IF statement based on the color of the cell.

Code:

Sub ForLoopswithArrays()

    ' there are 10 elements; the index goes from 0-9
    Dim myArray(56) As Integer
    Dim i As Integer
    
    
    'Ubound works out the maximum index
    For i = 0 To UBound(myArray)
        myArray(i) = i
    Next i

    Dim x As Integer
    For x = 0 To UBound(myArray)
        ThisWorkbook.Sheets("Sheet1").Cells(x + 1, 1).Value = myArray(x)
        ThisWorkbook.Sheets("sheet1").Cells(x + 1, 1).Font.ColorIndex = x
        ThisWorkbook.Sheets("sheet1").Cells(x + 2, 1).Interior.ColorIndex = x
    Next x
    
    
Dim y As Integer

y = 1

Do Until y = 56
Range("b" & y).Value = 2087
If Range("A" & y).Font.ColorIndex = 12 Then
Range("c" & y).Value = Range("a" & y).Value * Range("b" & y).Value
Range("c" & y).Interior.ColorIndex = 12
End If
y = y + 1
Loop

    

End Sub

Comments are closed.