+ Reply to Thread
Results 1 to 6 of 6

Get matches from A B rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2021
    Location
    Spain
    MS-Off Ver
    2016
    Posts
    12

    Post Get matches from A B rows

    Hello friends of this forum, I haven't bothered you with my stories for a long time, but I have a question to program a function in Excel and so, I came to this site from which I have always obtained the best and most professional answers.

    As always and in advance I thank you for all your help and the time you give me, if my doubt is not resolved here, I am convinced that I will not obtain it elsewhere.

    And of course I have tried, and I share with you the code that I have done, but it doesn't work, and the *xlsm document where you can see a small table and a brief explanation of what the function should do and I can't get it to do.


    Let's go to trouble


    A Function named "matches"
    A table like this with two columns A and B and down up to 45500 rows

    A B C
    411 203 no_match
    411 268 no_match
    411 342 472
    411 378 no_match
    417 309 no_match
    417 339 no_match
    437 321 no_match
    437 411 no_match
    451 242 no_match
    451 342 no_match
    451 413 no_match
    457 203 no_match
    457 435 no_match
    459 342 no_match
    459 203 no_match
    459 420 no_match
    462 371 no_match
    462 459 no_match
    472 342 no_match
    472 411 no_match
    477 217 no_match
    477 203 no_match

    We call the function from any cell in column C, for example let's start from C2

    The Function start the searh with A2 value and look for this value ONLY IN COLUMN B
    If value is found in other place or places in B column keeps in memory the value of the cell in the left of the found value, it means, the value in Column A. Following with the example we could find the A2 value(411) in B9 and in B21 and keep in memory the values (A9=437 and A21=472)

    Now we start the second searh with B2 value and look for this value ONLY IN COLUMN B, (but never look for it in the row where we called the function in this case function is called from C2 then row2 is out of search this time). If value is found in other place or places in B column keeps in memory the value of the cell in the left of the found value, it means, the value in Column A. Following with the example we could find the B2 value(203) in B13, B16 and B23 and keep in memory the values (A13=457, A16=459 and A23=477)

    Now compare the values saved in memory from the search with A2 (437 and 472) and the values saved in memory from the search with B2 (457, 459 and 477). If some value in the group from A2 share the same value with the others in the group from B2 a match is found and the function put this shared value in C2, but in this case as can be seen, no match is found, then the program can show in C2 "0" or "no_match" or any other message to show that there is no parity.

    Now if the same search is performed in C4, we can see than A4 value = 411 and B4 value = 342 are sharing one value in A column
    for A4 = B9 = B21 = 411 and B9 in A9 = 437 and B21 in A21 = 472
    for B4 = B11 = B15 = B20 = 342 and B11 in A11 = 451, B15 in A15 = 459 and B20 in A20 = 472
    So a match is found, the number 472 exist in both groups from the two initial numbers A4 and B4, then the function put number 472 in C4.

    When I call the function from any cell in column C, the answer is always the same "#?VALUE!" and never find a match but really exist in this table.

    Due to the characteristics of this table, we don't get two or more coincidences with the row values of A and B. This means that a group formed by A, B and C will be unique and will not be repeated.



    And this is the Function in which I've been working during some days and due to my little knowledge to program in this environment, I am unable to find where is the mistake so that the code works as it has to do.

    Function matches()
        Dim cellAB As Variant, cellBB As Variant
        Dim searchValueA As Variant, searchValueB As Variant
        Dim matchValueAB As Variant, matchValueBB As Variant
        Dim matchCellAB As Variant, matchCellBB As Variant
        
        searchValueA = ActiveCell.Offset(0, -2).Value
        searchValueB = ActiveCell.Offset(0, -1).Value
        
        For Each cellAB In ActiveCell.Offset(0, -1).EntireColumn
            If cellAB.Address <> ActiveCell.Address Then ' Exclude current cell
                If cellAB.Value = searchValueA Then
                    Set matchCellAB = cellAB
                    matchValueAB = matchCellAB.Offset(0, -1).Value
                    Exit For
                End If
            End If
        Next cellAB
        
        For Each cellBB In ActiveCell.Offset(0, -1).EntireColumn
            If cellBB.Address <> ActiveCell.Address Then ' Exclude current cell
                If cellBB.Value = searchValueB Then
                    Set matchCellBB = cellBB
                    matchValueBB = matchCellBB.Offset(0, -1).Value
                    Exit For
                End If
            End If
        Next cellBB
        
       
        If matchValueAB = matchValueBB Then
            ActiveCell.Value = matchValueAB
        Else
            ActiveCell.Value = "no-match"
        End If
    
    End Function

    Thanks in advance to all of you who in one way or another can help me debug my code so that this function can runs correctly in my spreadsheet.
    Attached Files Attached Files
    Last edited by SerenaCruz; 05-27-2023 at 07:07 AM. Reason: My question have been solved

  2. #2
    Registered User
    Join Date
    05-12-2021
    Location
    Spain
    MS-Off Ver
    2016
    Posts
    12

    Re: Get matches from A B rows

    I'm so sorry, I put this post in Functions because that's where it has to go, I wanted to delete this post, but I don't have permissions to do this, but the staff deleted the functions post and left this post that doesn't refer to a Macro but to a VBA function, it was my fault, I wanted to go too fast and I made a mistake by posting in the wrong place, I hope you can forgive me this time.

  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Get matches from A B rows

    Hi Serena. Try with:
    Attached Files Attached Files
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  4. #4
    Registered User
    Join Date
    05-12-2021
    Location
    Spain
    MS-Off Ver
    2016
    Posts
    12

    Re: Get matches from A B rows

    Quote Originally Posted by beyond Excel View Post
    Hi Serena. Try with:
    All working fine, thank you so much, perhaps some day i will rearch your high skills programming in VBA as you do.

    Now I tick the post as SOLVED, your contribution has helped me enormously. I wish you the best in your full life.

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Get matches from A B rows

    Quote Originally Posted by SerenaCruz View Post
    Now I tick the post as SOLVED, your contribution has helped me enormously. I wish you the best in your full life.
    Frankly, Serena, you have been very kind and I wish the same for you.

  6. #6
    Registered User
    Join Date
    05-12-2021
    Location
    Spain
    MS-Off Ver
    2016
    Posts
    12

    Re: Get matches from A B rows

    Quote Originally Posted by beyond Excel View Post
    Hi Serena. Try with:
    Finally, I’ve rearranged my Function to work properly, since some values could not be calculated with the script that you'd given me, anyway I greatly appreciate all your help and I have been able to learn some good techniques to continue progressing in this programming environment. Greetings and sure we'll meet again on the forum.
    And this is the Function in which I’ve been working during a week and finally works very fast and fine as it has to do. I share with all of you because I think is an interesting Function to work with, so someone else could use it for future projects. This is my favorite place in search for Excel doubts, and without doubt I will be aware of all the news that can help me and be useful to me and others users.

    Function Triplets(ByVal cela_objectiu As Range, ByVal rang_dades As Range) As Variant
        Dim dades_cerca As Range
        Dim grup_cerca_valors_A As Object, grup_cerca_valors_B As Object
        Dim primer_valor As Long, segon_valor As Long
        
        Set grup_cerca_valors_A = CreateObject("Scripting.Dictionary")
        Set grup_cerca_valors_B = CreateObject("Scripting.Dictionary")
        
        primer_valor = cela_objectiu.Offset(0, -2).Value ' Valors de la columna A, dos llocs a l'esquerra d'on cridem la funció
        segon_valor = cela_objectiu.Offset(0, -1).Value ' Valors de la columna B, un lloc a l'esquerra d'on cridem la funció.
        
        ' realitzem la cerca del primer valor, el de la columna A
        For Each dades_cerca In rang_dades.Columns(2).Cells
            If dades_cerca.Row > cela_objectiu.Row Then
                If dades_cerca.Value = primer_valor Then
                    grup_cerca_valors_A(dades_cerca.Offset(0, -1).Value) = True ' Afegim el valor trobat en la columna A al set de valors trobats pel grup A
                End If
            End If
        Next dades_cerca
        
        ' realitzem la cerca del segon valor, el de la columna B
        For Each dades_cerca In rang_dades.Columns(2).Cells
            If dades_cerca.Row > cela_objectiu.Row Then
                If dades_cerca.Value = segon_valor Then
                    grup_cerca_valors_B(dades_cerca.Offset(0, -1).Value) = True ' Afegim el valor trobat en la columna A al set de valors trobats pel grup B
                End If
            End If
        Next dades_cerca
        
        ' busquem les possibles coincidencies entre els dos grups de cerca el grup pels valors d'A i el grup pels valors de B
        For Each nombre In grup_cerca_valors_A.Keys
            If grup_cerca_valors_B.Exists(nombre) Then
                Triplets = nombre ' retorna el valor numèric si hi ha una coincidència
                Exit Function
            End If
        Next nombre
        
        Triplets = "no_trobat" ' Si no es troba cap coincidència, retorna el text "no_trobat"
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Compare two tables, find matches, and extract rows based on matches.
    By arcticspace in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-21-2022, 06:35 AM
  2. [SOLVED] Summing wildcard matches while subtracting wildcard matches of the previous rows
    By Alzabo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-03-2020, 05:00 PM
  3. [SOLVED] Counting wildcard matches while subtracting wildcard matches of the previous rows
    By Alzabo in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-24-2020, 02:40 PM
  4. Matches for 50,000 rows
    By stephme55 in forum Excel General
    Replies: 13
    Last Post: 08-19-2016, 12:06 AM
  5. Highlight rows if A matches but B is different
    By SpeedyOne18 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-07-2013, 08:05 PM
  6. Replies: 3
    Last Post: 01-31-2013, 04:33 AM
  7. Compare rows, cut matches to sheet, delete original rows
    By somesoldiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2010, 11:42 AM

Tags for this Thread

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