Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9109425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:02:41+00:00 2026-06-17T03:02:41+00:00

I wanted to color highlight cells that are different from each other; in this

  • 0

I wanted to color highlight cells that are different from each other; in this case colA and colB. This function works for what I need, but looks repetitive, ugly, and inefficient. I’m not well versed in VBA coding; Is there a more elegant way of writing this function?

EDIT
What I’m trying to get this function to do is:
1. highlight cells in ColA that are different or not in ColB
2. highlight cells in ColB that are different or not in ColA

    Sub compare_cols()

    Dim myRng As Range
    Dim lastCell As Long

    'Get the last row
    Dim lastRow As Integer
    lastRow = ActiveSheet.UsedRange.Rows.Count

    'Debug.Print "Last Row is " & lastRow

    Dim c As Range
    Dim d As Range

    Application.ScreenUpdating = False

    For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
        For Each d In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells
            c.Interior.Color = vbRed
            If (InStr(1, d, c, 1) > 0) Then
                c.Interior.Color = vbWhite
                Exit For
            End If
        Next
    Next

    For Each c In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells
        For Each d In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
            c.Interior.Color = vbRed
            If (InStr(1, d, c, 1) > 0) Then
                c.Interior.Color = vbWhite
                Exit For
            End If
        Next
    Next

Application.ScreenUpdating = True

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:02:42+00:00Added an answer on June 17, 2026 at 3:02 am

    Ah yeah that’s cake I do it all day long. Actually your code looks pretty much like the way I’d do it. Although, I opt to use looping through integers as opposed to using the “For Each” method. The only potential problems I can see with your code is that ActiveSheet may not always be “Sheet1”, and also InStr has been known to give some issues regarding the vbTextCompare parameter. Using the given code, I would change it to the following:

    Sub compare_cols()
    
        'Get the last row
        Dim Report As Worksheet
        Dim i As Integer, j As Integer
        Dim lastRow As Integer
    
        Set Report = Excel.Worksheets("Sheet1") 'You could also use Excel.ActiveSheet _
                                                if you always want this to run on the current sheet.
    
        lastRow = Report.UsedRange.Rows.Count
    
        Application.ScreenUpdating = False
    
        For i = 2 To lastRow
            For j = 2 To lastRow
                If Report.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
                    If InStr(1, Report.Cells(j, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then
                        'You may notice in the above instr statement, I have used vbTextCompare instead of its numerical value, _
                        I find this much more reliable.
                        Report.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background
                        Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                        Exit For
                    Else
                        Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
                        Report.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
                    End If
                End If
            Next j
        Next i
    
        'Now I use the same code for the second column, and just switch the column numbers.
        For i = 2 To lastRow
            For j = 2 To lastRow
                If Report.Cells(i, 2).Value <> "" Then
                    If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
                        Report.Cells(i, 2).Interior.Color = RGB(255, 255, 255) 'White background
                        Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
                        Exit For
                    Else
                        Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
                        Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
                    End If
                End If
            Next j
        Next i
    
    Application.ScreenUpdating = True
    
    End Sub
    

    Things I did differently:

    1. I used my integer method described above (as opposed to the ‘for each’ method).
    2. I defined the worksheet as an object variable.
    3. I used vbTextCompare instead of its numerical value in the InStr function.
    4. I added an if statement to omit blank cells. Tip: Even if only one
      column in the sheet is extra long (e.g., cell D5000 was accidentally
      formatted), then the usedrange for all columns is considered 5000.
    5. I used rgb codes for the colors (it’s just easier for me since I
      have a cheat sheet pinned to the wall next to me in this cubicle
      haha).

    Well that about sums it up. Good luck with your project!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just wanted to ask where I define initial class properties? From other languages I
I wanted a clickable TextView that changes color and holds it, that changes previously
I wanted to design a customized text selector that changed the text color when
just wanted to add this Color Picker ( http://code.google.com/p/devmil-android-color-picker/source/browse/ ) to my app and
I wanted to know that how can i set the color of the text
I wanted to make this script work somehow, but i find that after you
So I wanted to make every word in the text different color, but I've
Say I wanted to change the text color to red for a dropdown that
I wanted to remove UIImage background color and make it transparent... I tried this
I wanted to highlight different indentation levels in vim, so I could identify large

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.