Highlight Non Adjacent Cells and Total Their Values

We can highlight non-adjacent cells and total their values automatically with VBA. This strategy helps to perform automatic calculations and analysis on worksheet data quickly and easily.

Watch this training video on YouTube.

Here’s the complete VBA code:

Sub highlightNonAdjacentCells()
For i = 2 To 8
If Cells(i, 1) = “HDD” Then
Cells(i, 1).Font.ColorIndex = 2
Cells(i, 6).Font.ColorIndex = 2
Cells(i, 1).Interior.ColorIndex = 5
Cells(i, 6).Interior.ColorIndex = 5
End If

Next i

End Sub

Sub addValuesColoredCells()
Dim total As Single
Dim lastrow As Long
lastrow = Sheets(“sheet1”).Range(“A” & Rows.Count).End(xlUp).Row
total = 0
For i = 2 To lastrow
If Cells(i, 6).Interior.ColorIndex = 5 Then
total = total + Cells(i, 6).Value
End If
Next i
MsgBox “The total value is ” & total
End Sub

Here’s an image of the Excel worksheet with highlighted data:

Highlight Non-Adjacent Cells and Total their Values with VBA

Highlight Non-Adjacent Cells and Total their Values with VBA

Further reading:

Color Functions In Excel

Leave a Reply

Your email address will not be published. Required fields are marked *