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

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.