Consulting

Results 1 to 4 of 4

Thread: Compare two lists of numbers

  1. #1
    VBAX Regular
    Joined
    Jan 2017
    Posts
    29
    Location

    Question Compare two lists of numbers

    See attached sample data. Simply want to highlight numbers in COLUMN B that do not show up in COLUMN A. Would prefer VBA over conditional formatting.

    Thanks

    Samle Data.xlsx

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sub Main()  
      Dim hc&, c As Range, r As Range
      
      Set r = Range("B1", Cells(Rows.Count, "B").End(xlUp))
      hc = xlNone   'interior Fill color
      r.Interior.ColorIndex = hc
      
      Set r = Range("A1", Cells(Rows.Count, "A").End(xlUp))
      hc = vbRed  'interior fill color
      For Each c In Range("B1", Cells(Rows.Count, "B").End(xlUp))
        If WorksheetFunction.CountIf(r, c) = 0 Then _
          c.Interior.Color = hc
      Next c
    End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Probably the simplest way


    Option Explicit
    
    'Simply want to highlight numbers in COLUMN B that do not show up in COLUMN A. Would prefer VBA over conditional formatting.
    Sub Demo()
        Dim rLookHere As Range, rCheck As Range, rCell As Range
        Dim i As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            Set rLookHere = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
            Set rCheck = Range(.Cells(1, 2), .Cells(1, 2).End(xlDown))
        End With
        
        For Each rCell In rCheck.Cells
            i = -1
            On Error Resume Next
            i = Application.WorksheetFunction.Match(rCell.Value, rLookHere, 0)
            On Error GoTo 0
            
            If i = -1 Then rCell.Interior.Color = vbRed
            
        Next
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Jan 2017
    Posts
    29
    Location
    Thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •