+ Reply to Thread
Results 1 to 11 of 11

Match two Arrays from different worksheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Match two Arrays from different worksheets

    hello,

    I want to create a macro that matches each record from a worksheet I call "ATemplate" with another worksheet I call "BTemplate" using an unique ID (the ID row title is called "AT2RecID") that exists in both worksheets. For the sake of simplicity I only used several records as opposed to the thousands that I have in both worksheets. The workbook I provide with this post includes the following worksheets:

    - ATemplate
    - BTemplate
    - Output Results

    What I want the Macro to create in a new worksheet is the example found under worksheet "Output Results". In worksheet "Output Results" row titles should start IN ROW 2 and the data should start IN ROW 3. Row Titles B THROUGH M along with its associated data from worksheet "ATemplate" come over to the "Output Results" worksheet and Row Titles O and P along with its associated data from worksheet "BTemplate" come over to the "Output Results" worksheet. Again the records are matched up by the row title "AT2RecID" found in both worksheets "ATemplate" and "BTemplate". I understand I can do this in Access, but I would really like to see how it works in VBA.

    thanks much!
    Attached Files Attached Files
    Last edited by boldcode; 03-25-2013 at 01:49 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Match two Arrays from different worksheets

    Try this code

    Sub copyfrom()
    
        Dim c3 As Range, c4 As Range, LR2 As String, LR1&, ms As Worksheet, At As Worksheet, Bt As Worksheet
        
        Application.ScreenUpdating = 0
        Application.EnableEvents = 0
        
        On Error Resume Next
        
        If Not Evaluate("ISREF('Output Results'!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Output Results"
        Else
            Set ms = Sheets("Output Results")
            ms.Cells.ClearContents
       
       End If
        Set At = Sheets("ATemplate")
        Set Bt = Sheets("BTemplate")
        
    With Bt
    
         LR2 = Bt.Cells(Rows.Count, 1).End(xlUp).Row
         LR1 = At.Cells(Rows.Count, 1).End(xlUp).Row
        
         At.Range("B1").Resize(, 12).Copy ms.Range("A2")
        .Range("O1").Resize(, 2).Copy ms.Range("M2")
        
        For Each c3 In .Range("B2:B" & LR2)
        
         Set c4 = At.Range("B2:B" & LR1).Find(c3, , xlValues, xlWhole)
        
            If Not c4 Is Nothing Then
               c4.Resize(, 12).Copy ms.Range("A" & Rows.Count).End(xlUp).Offset(1)
               c3.Offset(, 13).Resize(, 10).Copy ms.Cells(Rows.Count, 13).End(xlUp).Offset(1)
            End If
        Next
    End With
           Application.ScreenUpdating = 1
           Application.EnableEvents = 1
           Set ms = Nothing: Set At = Nothing: Set Bt = Nothing
    End Sub
    Last edited by AB33; 03-23-2013 at 04:22 PM.

  3. #3
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    AB33,

    The code seem to work just fine; is there some code that will create a new worksheet "Output Results" and put the results there as oppose to me creating "Output Results" manually in order for the code to work.

    Thank you for your help AB33.

    - BC

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Match two Arrays from different worksheets

    Please see above amended code.
    Last edited by AB33; 03-23-2013 at 04:25 PM.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Match two Arrays from different worksheets

    Another way

    Option Explicit
    Sub abc()
     Const shA As String = "ATemplate"
     Const shB As String = "BTemplate"
     Const shResults As String = "Output Results"
     
     Dim aArr, w, i As Long, ii As Long, n As Long
     Dim aOutput()
     Dim dic As Object
     
     With Worksheets(shA)
        aArr = .Range("a1").CurrentRegion
     End With
     
     Set dic = CreateObject("scripting.dictionary")
     
     ReDim w(UBound(aArr, 2) - 1)
     With dic
        .comparemode = 1
        For i = 2 To UBound(aArr)
            For ii = 2 To UBound(aArr, 2)
                w(ii - 2) = aArr(i, ii)
            Next
            .Item(aArr(i, 2)) = w
        Next
     End With
     
     With Worksheets(shB)
        aArr = .Range("a1").CurrentRegion
     End With
     
     ReDim aOutput(1 To UBound(aArr), 1 To 14)
     With dic
        For i = 2 To UBound(aArr)
            If .exists(aArr(i, 2)) Then
                n = n + 1: w = .Item(aArr(i, 2))
                For ii = 0 To UBound(w)
                    aOutput(n, ii + 1) = w(ii)
                Next
                aOutput(n, 13) = aArr(i, 15)
                aOutput(n, 14) = CDbl(aArr(i, 16))
            End If
            
        Next
     End With
        
     If Not Evaluate("ISREF('" & shResults & "'!A1)") Then
        With Worksheets.Add(After:=Worksheets(Worksheets.Count))
            .Name = shResults
        End With
     End If
    
     With Worksheets(shResults)
        .Cells.Delete
        Worksheets(shA).Range("b1:m1").Copy .Range("a2")
        Worksheets(shB).Range("o1:p1").Copy .Range("m2")
        .Range("a3").Resize(n, UBound(aOutput, 2)) = aOutput
     End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    mike7952,

    I will test out your code and let you know where I stand. Thanks for the help.

    - BC

  7. #7
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    Mike7952,

    Is there any way to change your code so that results are displayed in the Active Worksheet as opposed to a predifined one.

    Thanks,

    Bc

  8. #8
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    mike7952,

    I figured out how to use the ActiveSheet so no worries there. Question about your variables, how come variables aArr, w, and aOutput() are not defined as anything. I would like to define them so they are not Variant or defaulted to Variant.

    - BC

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Match two Arrays from different worksheets

    BC,

    They need to be variant.

  10. #10
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    Mike,

    I figured out with the those variant variables mean, but I can't figure out what the "n" variable variant does. I was wondering if you could shed some insight on that particular variable.

    - BC

  11. #11
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Match two Arrays from different worksheets

    Mike,

    I figured it out. Thank you for your solution!

    - Bc

+ 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