October 31, 2015

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

One thought on “Highlight Non Adjacent Cells and Total Their Values

  1. Resp. Sir,
    I had written you previously also. I want to generate invoice reports from different invoice templates. I have tried our LoopthroughDirectory module and Invoice Looping also. Every time I am failed. From the last 3 months I am on this work. So please help me. I will send you my sample invoice templates and my zmaster file also. please show me the proper coading. Sir please help me.

Comments are closed.