+ Reply to Thread
Results 1 to 10 of 10

Thread: Matching >1 value in a loop?

  1. #1
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    62

    Matching >1 value in a loop?

    Hi guys,

    If the following macro matchs two ID numbers from column B in column C it shoud highlight them green.

    Column C may has multiple entries of column B, however the macro i have written appears to only match the first ID number found in column C from B.

    I think this may have something to do with my loop, can anyone suggest a solution?

    Sub colour_filter()
    
    Dim myrange As Range
    Dim Rng As Range
    Dim rng1 As Range
    Dim a As Integer
       
        Set Rng = ActiveSheet.Range("B2", Range("B" & Rows.Count).End(xlUp))
        Set rng1 = ActiveSheet.Range("C2", Range("C" & Rows.Count).End(xlUp))
        a = 0
       
        For Each myrange In Rng
          If IsNumeric(Application.Match(myrange.Value, rng1, 0)) Then
             rng1(Application.Match(myrange.Value, rng1, 0), 1).Range _
             ("A1", Cells(1, Columns.Count).End(xlToLeft)).Interior.color = vbGreen
             a = a + 1
          End If
        Next myrange
       
    MsgBox "Number of matches: " & a
    
    End Sub
    Many thanks,

    D
    Last edited by dems; 12-27-2009 at 08:26 PM.

  2. #2
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    62

    Re: Matching >1 value in a loop?

    Thinking about the above, I have switched the way the loop works so that it reads column C to B (rather than visa-versa).

    However, the problem now is that the macro simply colours the row for the block of cells within column B [ie. ActiveSheet.Range("B2", Range("B" & Rows.Count).End(xlUp))] across the rows. Clearly not the desired result...

    Sub colour_filter()
     
    Dim myrange As Range
    Dim Rng As Range
    Dim rng1 As Range
    Dim a As Integer
     
        Set Rng = ActiveSheet.Range("B2", Range("B" & Rows.Count).End(xlUp))
        Set rng1 = ActiveSheet.Range("C2", Range("C" & Rows.Count).End(xlUp))
        a = 0
     
        For Each myrange In rng1
          If IsNumeric(Application.Match(myrange.Value, Rng, 0)) Then
             rng1(Application.Match(myrange.Value, Rng, 0), 1).Range _
             ("A1", Cells(1, Columns.Count).End(xlToLeft)).Interior.color = vbGreen
             a = a + 1
          End If
        Next myrange
     
    MsgBox "Number of matches: " & a
     
     
    End Sub

  3. #3
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Matching >1 value in a loop?

    Hi dems
    cant you just use conditional formating?
    something like
    Forumlais=countif($B$1:$C$100),A1)>1
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  4. #4
    Forum Guru Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007
    Posts
    3,523

    Re: Matching >1 value in a loop?

    The suggestion to use CF was made in this original post - with no response from the OP.
    VBA search and highlight macro
    Thread is now locked.

    A sample workbook has been requested twice - no response. It would save us all some time.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  5. #5
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Matching >1 value in a loop?

    oh try something like...
    Sub ptest()
        Dim ws1 As Worksheet
        Dim FoundOne As Range, LookInR As Range, LookForR As Range, c As Range
        Dim fAddress
        Application.ScreenUpdating = False
        Set ws1 = Sheets("Sheet1")
         Set LookInR = ws1.Range(ws1.Range("c1"), ws2.Range("c" & Rows.Count).End(xlUp))
        Set LookForR = ws1.Range(ws1.Range("b1"), ws2.Range("b" & Rows.Count).End(xlUp))
        For Each c In LookForR
            With LookInR
                Set FoundOne = .Find(What:=c, lookat:=xlPart)
                If Not FoundOne Is Nothing Then
                    fAddress = FoundOne.Address
                    Do
                     c.Resize(1, 1).Interior.Color = vbGreen
                     FoundOne.Resize(1, 1).Interior.Color = vbGreen
                        Set FoundOne = .FindNext(After:=FoundOne)
                    Loop While FoundOne.Address <> fAddress
                End If
            End With
        Next c
        Set ws1 = Nothing
        Set LookInR = Nothing: Set LookForR = Nothing
        Application.ScreenUpdating = True
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  6. #6
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    62

    Re: Matching >1 value in a loop?

    Hi Pike/Palmetto. Let me explain why I think I require VBA instead of conditional formatting.

    The data is spread over 3 sheets and contains ~150,000 rows. I was hoping to use this code to highlight the required fields and then copy and paste them with a seperate macro (applying similar coding) to a fresh sheet.

    Following that I need to match the ripped data based on another parameter (dates) to further filter the results. Once more, I intended to apply the similar code to achieve this.

    Palmetto I am reluctant to provide a workbook sample, I will see what I can do.

  7. #7
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Matching >1 value in a loop?

    something like
    Sub ptestp()
        Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
        Dim FoundOne As Range, LookInR As Range, LookForR As Range, c As Range
        Dim nr3    As Long, fAddress
        Application.ScreenUpdating = False
        Set ws1 = Sheets("Sheet1")
        Set ws2 = Sheets("Sheet2")
        Set ws3 = Sheets("Sheet3")
        Set LookInR = Range(ws1.Range("A1"), ws1.Range("B" & Rows.Count).End
        Set LookForR = Range(ws2.Range("A1"), ws2.Range("C" & Rows.Count).End(xlUp))
        nr3 = ws3.Range("A" & Rows.Count).End(xlUp).Row + 1
        For Each c In LookForR
            With LookInR
                Set FoundOne = .Find(What:=c, lookat:=xlPart)
     
                If Not FoundOne Is Nothing Then
                    fAddress = FoundOne.Address
                    Do
                        'FoundOne.EntireRow.Copy Destination:=ws3.Cells(nr3, 1)
                        FoundOne.Offset(0, 1).Copy Destination:=ws3.Cells(nr3, 4)    '.Value = DXFDict.Item(Key)
                        nr3 = nr3 + 1
                        Set FoundOne = .FindNext(After:=FoundOne)
                    Loop While FoundOne.Address <> fAddress
                End If
            End With
        Next c
        Set ws1 = Nothing
        Set ws2 = Nothing
        Set ws3 = Nothing
        Set LookInR = Nothing: Set LookForR = Nothing
        Application.ScreenUpdating = True
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  8. #8
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    62

    Re: Matching >1 value in a loop?

    Hey Pike,

    Wow that is really appreciated!

    I am having a couple of problems applying the code though, and would further appreciate your help - if you don't mind.

    When i step through the code i dont appear to have any problems until I run it whereby nothing appears to happen. The debugger doesn't appear until I escape the code, which then informs me it is 'Unable to get the FindNext property of the range class'

    May I also confirm that the fAdress variable should be a range?

    Many thanks,

    D

  9. #9
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Matching >1 value in a loop?

    What I see is this
        Set LookInR = Range(ws1.Range("A1"), ws1.Range("B" & Rows.Count).End
    Some code has disapeared (End(xlup)) ?? as well as like End Sub ??

    ... and fAddress is a String

    Pikes code works but it seems a bit strange to me that
    LookInR is in Sheet1 Column 1 & 2
    LookForR is in Sheet2 Columns 1,2,3
    FoundOne is set (pointer) to the cell in Sheet1 (LookInR)
    and then
    FoundOne.Offset(0, 1).Copy Destination:=ws3.Cells(nr3, 4)

    If LookinR was a single column and you need to look up the ID in that columns and copy the data to the right of the found ID to Sheet3, that would make sence.

    Now
    When the ID is found in Column 1 the data on the right is also an ID ??

    Summarizing:
    Why should the LookForR and LookIn R be multidimensional. I would say these are all one dimension like in Pike's first approach?
    Last edited by rwgrietveld; 12-24-2009 at 05:14 AM.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  10. #10
    Registered User
    Join Date
    06-26-2009
    Location
    O.o
    MS-Off Ver
    Excel 2003
    Posts
    62

    Re: Matching >1 value in a loop?

    Finally managed to get my head around the problem using a nested loop. Applied code for future reference below:

    Sub FilterV2()
    
    Dim Rng, rng1 As Range
    Dim a As Double
    Dim intcounter, intcounter1 As Double
    Dim intNumberOfRecords, intNumberOfRecords1 As Double
    
    Set Rng = ActiveSheet.Range("B2", Range("B" & Rows.Count).End(xlUp))
    Set rng1 = ActiveSheet.Range("C2", Range("C" & Rows.Count).End(xlUp))
    
    
    intNumberOfRecords = Rng.Rows.Count - 1
    intNumberOfRecords1 = rng1.Rows.Count - 1
    a = 0   
    
        For intcounter = 0 To intNumberOfRecords
          For intcounter1 = 0 To intNumberOfRecords1
                If (Rng(intcounter) = rng1(intcounter1)) Then
                    Rows(intcounter1 + 1).Select
                    Selection.Copy
                    Worksheets("Filter").Select
                    ActiveSheet.Range("C65536").End(xlUp).Offset(1, 0).Select
                    ActiveSheet.Paste Link:=True
                    Worksheets("Sheet 1").Select
                    a = a + 1
                End If
          Next
        Next
         
    MsgBox "Number of matches: " & a
    
    End Sub
    A little brutish is selecting rows, since I couldn't get this to work dynamically, ie the following wouldnt work.

    Range(intcounter + 1, Cells(1, Columns.Count).End(xlToLeft)).Select
    Also less automated as you have to execute the code between different sheets and update the macro's return reference.

    Otherwise, thank's everyone for taking the time to help.
    Last edited by dems; 12-28-2009 at 01:53 AM.

+ 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.2.0