+ Reply to Thread
Results 1 to 3 of 3

Matching Values in Columns problems

  1. #1
    louie
    Guest

    Matching Values in Columns problems

    Hello,
    I have written a macro that matches values in two columns and highlights the
    matches. The problem arises in a loop that is searching in one column has
    no match in the other. The code generates an error. Any input appreciated,
    code follows.

    Sub Match()
    'Compares values in 2 columns and highlights if the same

    For Nextloop = 1 To 50
    ActiveCell.Offset(1, 0).Activate
    i = 1
    Do Until ActiveCell.Value = Range("b1").Offset(i, 0)
    i = i + 1
    Loop
    If ActiveCell.Value = Range("b1").Offset(i, 0) Then
    Range("b1").Offset(i, 0).Cells.Interior.ColorIndex = 17
    If IsEmpty(ActiveCell) Then
    Exit Sub
    End If
    End If
    Next Nextloop
    End Sub



  2. #2
    Tom Ogilvy
    Guest

    Re: Matching Values in Columns problems

    Sub Match()
    Dim rng as Range, lastRow as Long
    Dim i as Long
    'Compares values in 2 columns and highlights if the same
    lastRow = cells(rows.count,"B").End(xlup).Row
    set rng = Range(ActiveCell, ActiveCell.End(xldown))
    for i = 1 to lastrow
    if Application.Countif(rng,cells(i,"B")) > 0 then
    Cells(i,"B").Interior.ColorIndex = 17
    Next i
    End Sub

    --
    Regards,
    Tom Ogilvy



    "louie" <[email protected]> wrote in message
    news:[email protected]...
    > Hello,
    > I have written a macro that matches values in two columns and highlights

    the
    > matches. The problem arises in a loop that is searching in one column has
    > no match in the other. The code generates an error. Any input

    appreciated,
    > code follows.
    >
    > Sub Match()
    > 'Compares values in 2 columns and highlights if the same
    >
    > For Nextloop = 1 To 50
    > ActiveCell.Offset(1, 0).Activate
    > i = 1
    > Do Until ActiveCell.Value = Range("b1").Offset(i, 0)
    > i = i + 1
    > Loop
    > If ActiveCell.Value = Range("b1").Offset(i, 0) Then
    > Range("b1").Offset(i, 0).Cells.Interior.ColorIndex = 17
    > If IsEmpty(ActiveCell) Then
    > Exit Sub
    > End If
    > End If
    > Next Nextloop
    > End Sub
    >
    >




  3. #3
    louie
    Guest

    Re: Matching Values in Columns problems

    Thanks Tom, your code works great. I will study it to help me learn better
    ways to programatically make life with excel easier. I added End If before
    Next i to complete your code in case anyone copies it for use.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1