Highlight Cell when Row data changes from previous Row

3gswish

New Member
Joined
Apr 28, 2011
Messages
29
Hi everyone,

I have some very basic scripting skills but they don't yet translate well to VBA so would very much appreciate the benefit of your knowledge.

I have a spreadsheet with over 6k rows, each with over 30 columns.

My focus is in 2 columns. ColumnA is formatted as General, ColumnB as Number.


ColumnAColumnB
05255088-0f15-48f1-a65e-affb6d583d351373064224475
05255088-0f15-48f1-a65e-affb6d583d351373064224475
05384219-f4ad-44de-9efc-d6526ad169411373064223701
05384219-f4ad-44de-9efc-d6526ad169411373064223701
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218

<tbody>
</tbody>


I need your assistance with VBA code that will Fill the cell in ColumnB if it changes within the same ColumnA data.


So the output would end up like this. (Although the cell would be filled, not the text turned red)

05255088-0f15-48f1-a65e-affb6d583d351373064224475
05255088-0f15-48f1-a65e-affb6d583d351373064224475
05384219-f4ad-44de-9efc-d6526ad169411373064223701
05384219-f4ad-44de-9efc-d6526ad1694112345
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
0539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
0539a867-1272-462d-ab34-fe3af4d0aa8b9876543
0539a867-1272-462d-ab34-fe3af4d0aa8b9876543

<tbody>
</tbody>


So it's pretty basic. If we simply remember the first ColumnA and ColumnB data, go to the next row, if ColumnA is the same, then ColumnB should be the same. If it's not, then fill with red, go to the next row, repeat. if ColumnA changes, remember the new pair, go to the next row and compare again.


I've thought about this from a scripting perspective, and imagine that something like this would do the job:

Code:
sub FindBadTermID ()
     Dim row As Integer
     Dim dataA as string, dataB as string
     Set row = 2

     newColumnALoop
          Set dataA = A(row), dataB=B(row)                         # Get the first pair in a new ColumnA group
          Set row=row+1
               testColunBLoop
                  if A(row) = Blank, end
                  if A(row) != dataA, goto newColumnALoop     # If it's a new ColumnA group, go back and get first pair
                  if B(row) != dataB, fill B(row)=red                # If ColumnB is different that ColumnB of the first pair, make the cell red
                  Set row=row+1
                  goto testColumnBLoop
   
End Sub


I've gotten a start on changing the script to VBA, but am now strugging. Would you please take a look and give me your thoughts and code suggestions?

Thank you very much!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,

You can do this with conditional formatting.

Select B1 and highlight down to the last data row in column B e.g. B1:B6000 (ensure B1 is the active cell).
Then apply conditional formatting using the following formula:

=B1 <> INDEX(B:B, MATCH(A1, A:A, 0))
 
Upvote 0
3gswish,

You screenshots were confusing. I hope I got it right.

Sample raw data:


Excel 2007
AB
1
205255088-0f15-48f1-a65e-affb6d583d351373064224475
305255088-0f15-48f1-a65e-affb6d583d351373064224475
405384219-f4ad-44de-9efc-d6526ad169411373064223701
505384219-f4ad-44de-9efc-d6526ad1694112345
60539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
70539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
80539a867-1272-462d-ab34-fe3af4d0aa8b9876543
90539a867-1272-462d-ab34-fe3af4d0aa8b9876543
10
Sheet1


After the macro:


Excel 2007
AB
1
205255088-0f15-48f1-a65e-affb6d583d351373064224475
305255088-0f15-48f1-a65e-affb6d583d351373064224475
405384219-f4ad-44de-9efc-d6526ad169411373064223701
505384219-f4ad-44de-9efc-d6526ad1694112345
60539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
70539a867-1272-462d-ab34-fe3af4d0aa8b1373064221218
80539a867-1272-462d-ab34-fe3af4d0aa8b9876543
90539a867-1272-462d-ab34-fe3af4d0aa8b9876543
10
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub MakeRed()
' hiker95, 07/07/2013
' http://www.mrexcel.com/forum/excel-questions/712453-highlight-cell-when-row-data-changes-previous-row.html
Dim r As Long, lr As Long, n As Long, i As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n > 1 Then
    For i = r + 1 To r + n - 1
      If Cells(i, 2) = Cells(r, 2) Then
        'do nothing
      Else
        Cells(i, 2).Interior.Color = 255
    End If
    Next i
  End If
  r = r + n - 1
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the MakeRed macro.
 
Upvote 0
3gswish,

I was confused by your macro code.

Is row 1 in your worksheet a blank row?

Or, does your raw data begin in row 1?
 
Upvote 0
Thanks circlechicken,

Gave it a shot, however it's highlighting the cell below the one that's different. Here is what I am getting.

Column A
Column B
0024e748-4bd6-4982-acf5-f6a24ccc2bee
1373064220804
0024e748-4bd6-4982-acf5-f6a24ccc2bee1234
002fe70b-9e6e-41ae-875a-87417f2a9d1c9023
003ac6fb-2cb5-4fd8-acb9-52ca9ad60dc51373064223647
003ac6fb-2cb5-4fd8-acb9-52ca9ad60dc51373064223647
004ebee3-719d-46c0-873b-638b5aeb92851373064223355
004ebee3-719d-46c0-873b-638b5aeb92854444
0050ba03-e1e4-4576-af3e-21d2490f2d0f1373064223627
0050ba03-e1e4-4576-af3e-21d2490f2d0f1373064223627
0058a170-16c9-4b81-8a96-c94b17fdd9d63754
00684eca-a3c0-4676-8149-a027893c7a5c12982
006ac36b-befa-46ee-871e-f2d6fd10967214398
007095a4-6f7e-4c00-a333-48360a2f21907600
0070a95d-4345-4efd-85e7-9e1e295e1ef95227
00718b1c-ab0a-49e0-aedd-c32b1d300d6a1373064222917
00718b1c-ab0a-49e0-aedd-c32b1d300d6a1373064222917

<tbody>
</tbody>


I will study your formula in the meantime :)

Thanks,
 
Upvote 0
It works for me. If your data starts from B2 rather than B1, you need to begin your selection from B2 (ensure B2 is the active cell) and adjust the formula as required:

=B2 <> INDEX(B:B, MATCH(A2, A:A, 0))
 
Upvote 0
Hello Hiker95,

Macro worked perfectly - Thank you!

In answer to your question below, row1 is a header.

The macro found 3 groups of ColumnA data that had different ColumnB values in a worksheet that contained a bit over 10k rows.

Couldn't have done it otherwise!

I am beginning my study of VBA and will definitely spend some time figuring out and digesting your approach. Much appreciated.
 
Upvote 0
Hi circledchicken,

AHA! That did the trick.

2 different approaches to study now...

Thanks!
 
Upvote 0
3gswish

Thanks for the feedback.

You are very welcome. Glad I could help.

Come back anytime.


I am beginning my study of VBA and will definitely spend some time figuring out and digesting your approach

See if something below will help:

Training / Books / Sites as of 6/26/2013

What Is VBA?
VBA is an acronym for Visual Basic for Applications. VBA should not be confused with VB, which is standard Visual Basic. Visual Basic for Applications is a programming feature designed by Microsoft for use with their Microsoft Office

MrExcel's Products: Books, CDs, Podcasts Discuss topics related to Holy Macro! Products: Learn Excel from MrExcel, VBA and Macros for Microsoft Excel,Holy Macro! It's 2500 VBA Examples CD, Guerilla Data Analysis Using Microsoft Excel and Excel Knowledge Base CD and the MrExcel Podcasts.
MrExcel's Products: Books, CDs, Podcasts

There are over 1800 Excel videos/tutorials here:
ExcelIsFun - YouTube

Getting Started with VBA.
DataPig Technologies

If you are serious about learning VBA try
Macros Made Easy for Microsoft Excel

Excel Tutorials and Tips - VBA - macros - training
Excel Tutorial | Excel Tips | Excel Articles

Microsoft Office training
http://office.microsoft.com/en-us/tr...101782702.aspx

Free VBA Course
Free VBA Course

Excel 2007 VBA materials to learn here:
VBA for Excel 2007 tutorial-VBA programming?

Here's a good primer on the scope of variables.
Understanding Scope

Using Variables in Excel VBA Macro Code
Excel VBA Variables. Using Variables in Excel VBA Macro Code

See David McRitchie's site if you just started with VBA
Getting Started with Macros and User Defined Functions

What is a Visual Basic Module?
What Is A VBA Module and how is a VBA Module Used?

Here is a good introductory tutorial using a VBA Class:
Classes In VBA

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

Ron's Excel Tips
Excel for Windows Tips

Ron de Bruin's Mail from Excel and make/mail PDF files (Windows)
Mail from Excel and make/mail PDF files (Windows)

Anthony's Excel VBA Page - Excel Application - Excel Consultant - Excel Consulting (see 3 tutorials in Excel VBA Basic Tutorial Series)

http://www.jlathamsite.com/Teach/VBA...troduction.pdf (95 page "book")

BET: Microsoft Excel Visual Basic

Creating An XLA Add-In For Excel, Writing User Defined Functions In VBA
Creating An XLA Add In

How do I create a PERSONAL.XLS(B) or Add-in
http://www.rondebruin.nl/personal.htm

Creating custom functions
Creating custom functions - Excel - Office.com

Writing Your First VBA Function in Excel
Writing Your First VBA Function in Excel

VBA for Excel (Macros)
Excel Macros (VBA) Tutorial

Excel Macros Tutorial
Excel Macros (VBA) Tutorial

Excel Macros & Programming
Learning about EXCEL macros

VBA Lesson 11: VBA Code General Tips and General Vocabulary
VBA for Excel macros language

Excel VBA -- Adding Code to a Workbook
Excel VBA -- Adding Code to Excel Workbook

Beyond Excel's recorder

Helpful as a simple concise reference for the basics, and, using the macro recorder is endlessly powerful.
MS Excel: Cells

Learn to debug:
Debugging VBA

How To: Assign a Macro to a Button or Shape
How To: Assign a Macro to a Button or Shape | Peltier Tech Blog | Excel Charts

User Form Creation
Create an Excel UserForm

When To Use a UserForm & What to Use a UserForm For
When to use Userform & What To Use Them For. Excel Training VBA 2 lesson 2

Excel Tutorials / Video Tutorials - Functions
Excel VLookup Function Examples

How to insert Buttons, radio buttons and check boxes in Excel
How to insert Buttons, radio buttons and check boxes in Excel - Bing Videos

INDEX MATCH - Excel Index Function and Excel Match Function
Excel Index Function and Match Function

Excel Data Validation
Excel Data Validation Tips and Quirks
Excel Data Validation - Add New Items

Excel -- Data Validation -- Create Dependent Lists
Excel Data Validation -- Dependent Lists

Your Quick Reference to Microsoft Excel Solutions
XL-CENTRAL.COM : For your Microsoft Excel Solutions

New! Excel Recorded Webinars
DataPig Technologies

Fuzzy Matching - new version plus explanation

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
Programming In The VBA Editor

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel 2007 Book: you can try this...there is a try before you buy ebook available at this link…

by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)
Professional Excel Development

by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)
Excel 2002 VBA: Programmers Reference

"Professional Excel Development" by Rob Bovey, Dennis Wallentin, Stephen Bullen, & John Green

DonkeyOte: My Recommended Reading, Volatility
Volatile Excel Functions -Decision Models

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
VBA Array Basics
VBA Arrays
Excel: Introduction to Array Formulas - Xtreme Visual Basic Talk
Excel VBA Chpter 21: Array in Excel VBA

Array Dimensions in Visual Basic - Working with Dimensions (code and graphics)
Array Dimensions in Visual Basic

Shortcut Keys in Excel 2000 through 2007

Pivot Intro
Using Pivot Tables and Pivot Charts in Microsoft Excel
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Getting Started with Pivot Tables
Excel Pivot Table -- Dynamic Data Source
Overview of PivotTable and PivotChart reports
Overview of PivotTable and PivotChart reports - Excel - Office.com
Build a Pivot Table in Excel VBA
Excel VBA - Build A Pivot Table In Excel VBA Tutorials

Email from XL - VBA
Mail from Excel example pages

Outlook VBA
Writing VBA code for Microsoft Outlook

Function Dictionary
http://www.xlfdic.com/

Function Translations
Excel 2007 function name translations - Dictionary Chart Front Page

Dynamic Named Ranges
Excel Names -- Excel Named Ranges

How to create Excel Dashboards
http://www.mrexcel.com/Excel-dashboards-Xcelsius.html
Excel Dashboard Templates
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Free Microsoft Excel Dashboard Widgets to Download
AJP Excel Information - Gauge

Excel Dashboard / Scorecard Ebook
Excel Dashboards and Scorecards Ebook | How to Create Dashboards in Excel

Mike Alexander from Data Pig Technologies
Excel 2007 Dashboards and Reports For Dummies: Michael Alexander: 9780470228142: Amazon.com: Books

Templates
CPearson.com Topic Index
Excel Template - Golf Scores
Free Microsoft Excel Template Links & Search Engine

Microsoft Excel Cascading Listboxes Tutorial
Microsoft Excel Cascading Listboxes Tutorial - YouTube

Date & Time stamping:
McGimpsey & Associates : Excel : Time and date stamps

Get Formula / Formats thru custom functions:
Show FORMULA or FORMAT of another cell

A nice informative MS article "Improving Performance in Excel 2007"
Improving Performance in Excel 2007

Progress Meters
AJP Excel Information - Progress meters
Website Disabled

How to convert text to numbers in Excel
How to convert text to numbers in Excel

How to parse data from the web - Brett Fret has a video in YouTube
Excel VBA - Easily parse XML data from a file or website using XML Maps - YouTube

And, as your skills increase, try answering posts on sites like:
MrExcel.com | Excel Resources | Excel Seminars | Excel Products
Excel Help Forum
Excel Templates | Excel Add-ins and Excel Help with formulas and VBA Macros
VBA Express Portal
Excel, Access, PowerPoint and Word VBA Macro Automation Help

If you are willing to spend money for the training, then something here should work for you...
Amazon.com: excel tutorial dvd
Amazon.com: excel tutorial dvd

Advanced Excel Training - Online Excel Course
http://www.udemy.com/advanced-excel/
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,655
Members
449,113
Latest member
Hochanz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top