+ Reply to Thread
Results 1 to 7 of 7

Need to create matched pairings between 2 columns

Hybrid View

Technetium Need to create matched... 06-27-2021, 01:23 PM
Artik Re: Need to create matched... 06-27-2021, 02:21 PM
Technetium Re: Need to create matched... 06-27-2021, 05:36 PM
torachan Re: Need to create matched... 06-28-2021, 03:56 AM
Artik Re: Need to create matched... 06-28-2021, 05:10 AM
torachan Re: Need to create matched... 06-28-2021, 07:39 AM
Artik Re: Need to create matched... 06-28-2021, 08:29 AM
  1. #1
    Registered User
    Join Date
    10-23-2017
    Location
    Boston, MA
    MS-Off Ver
    Excel 365
    Posts
    73

    Need to create matched pairings between 2 columns

    I have column A and column B that both have a list of names. I need to be able to create a list of all pairings names from A with names from B, and has to work for different numbers of names in each column. Ideally I want it set up so that I can add or delete a name from either column, run the VBA macro, and it will update all the pairings. The example below should show what I mean for the output:

    Well, it would, but it seems I can't directly paste it from the clipboard. I attached a file. If the description of what I want isn't clear above, hopefully someone takes the time to look at the file.

    I don't believe it is possible to do this without VBA, but I have been wrong about some other similar assumptions before, so if it can be done with normal Excel code, then that would definitely be preferable. Thanks! I'm pretty new to VBA, and while I tried to look up some examples of writing VBA code, I couldn't find anything that seemed remotely like this.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Need to create matched pairings between 2 columns

    e.g.
    Sub AAA()
      Dim vColA
      Dim vColB
      Dim vMatchups As Variant
      Dim lRow As Long
      Dim rA As Long, rB As Long
      Dim i As Long
      
      lRow = Cells(Rows.Count, "A").End(xlUp).Row
      vColA = Range("A2:A" & lRow).Value
      
      lRow = Cells(Rows.Count, "B").End(xlUp).Row
      vColB = Range("B2:B" & lRow).Value
      
      ReDim vMatchups(1 To UBound(vColA) * UBound(vColB), 1 To 2)
      
      For rA = 1 To UBound(vColA)
        For rB = 1 To UBound(vColB)
          i = i + 1
          vMatchups(i, 1) = vColA(rA, 1)
          vMatchups(i, 2) = vColB(rB, 1)
        Next rB
      Next rA
      
      Range("D2:E2").Resize(UBound(vMatchups)).Value = vMatchups
    End Sub
    Artik

  3. #3
    Registered User
    Join Date
    10-23-2017
    Location
    Boston, MA
    MS-Off Ver
    Excel 365
    Posts
    73

    Re: Need to create matched pairings between 2 columns

    Thank you very much. Since I am new to VBA it took me a little while to figure out what everything meant, but I did figure it out and was able to adapt it to my specific circumstance ( I did not need UBound or Rows.Count because there's an Excel cell that already has that calculated).

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Need to create matched pairings between 2 columns

    good old fashioned loops - easier to understand.
    torachan.
    Option Explicit
    Dim x As Long, y As Long, r1 As Long, r2 As Long, ctr As Long
    
    Sub MATCHUP()
    x = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
    y = Sheet1.Cells(Rows.Count, "B").End(xlUp).Row
    ctr = 2
    For r2 = 2 To x
    For r1 = 2 To y
    Sheet1.Cells(ctr, 8) = Sheet1.Cells(r2, 1)
    ctr = ctr + 1
    Next r1
    Next r2
    ctr = 2
    For r2 = 2 To x
    For r1 = 2 To y
    Sheet1.Cells(ctr, 9) = Sheet1.Cells(r1, 2)
    ctr = ctr + 1
    Next r1
    Next r2
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Need to create matched pairings between 2 columns

    Quote Originally Posted by torachan View Post
    good old fashioned loops - easier to understand.
    But several times slower in action

    Artik

  6. #6
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Need to create matched pairings between 2 columns

    I did not see 10,000 rows of data in the example therefore speed did not appear of the essence.
    Do not blink as you will not see the time difference.

    Attachment 738162
    Attachment 738163

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Need to create matched pairings between 2 columns

    OK, but by not practicing using arrays, we try to avoid such solutions. Someday it will be time to use 10k, 100k, 1M and what? We'll use cell loops because that's the only thing we know how to do. The forums are full of suboptimal code. We are to learn and not duplicate old uneconomical solutions.
    It's so off topic.

    Artik

+ 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. Replies: 7
    Last Post: 04-02-2020, 08:42 AM
  2. How to identify pairings (co-occurrences?) across multiple columns
    By samc411 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-26-2019, 04:12 AM
  3. [SOLVED] Return a value if matched on specific two columns.
    By pphg in forum Excel General
    Replies: 10
    Last Post: 03-31-2017, 03:49 AM
  4. Sorting matched data from two columns.
    By R3DAlert in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 02-16-2016, 04:56 PM
  5. [SOLVED] Create Data validation list from matched criteria value results
    By lukeeb in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-10-2014, 10:39 PM
  6. Replies: 4
    Last Post: 12-27-2013, 07:53 AM
  7. how to create a button that print out a file that data matched the input window
    By nan342 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-15-2013, 04:20 PM

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