+ Reply to Thread
Results 1 to 4 of 4

Separate and align rows from two separate sheets based on a cell value.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-14-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    15

    Separate and align rows from two separate sheets based on a cell value.

    Hello and thank you for any assistance you can provide.

    In the attached file, I have two sheets with data: Internal Source Data, and Outside Source Data.

    I have a tab titled Desired Result, which illustrates the end result I'm trying to achieve - which is basically to take the date from the Internal and Outside Source Data sheets, separate and align them based on the Unique ID.

    I have issues using Vlookup or Index/Match, because the amounts of rows for each Unique ID are not always congruent.

    Also, I have a macro (thank you jindon!) which fills in formulas in rows J through L. I can get them to populate in the appropriate areas, but the formulas are returning 0. I'm not sure what I'm missing there.

    Furthermore, is this something that is better handled by a Pivot Table, or some other means?
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Separate and align rows from two separate sheets based on a cell value.

    Here's my take on that:
    Option Explicit
    
    Sub Consolidate()
    Dim wsOUT As Worksheet, NR As Long, FR As Long, i1 As Long, i2 As Long, c As Long, LR As Long
    Dim MyARR1 As Variant, MyARR2 As Variant, Started As Boolean
    
    Application.ScreenUpdating = False
    
    With ThisWorkbook.Sheets("Internal Source Data")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        MyARR1 = .Range("A3:D" & LR).Value
        .Range("C:C").NumberFormat = "@"
    End With
    With ThisWorkbook.Sheets("Outside Source Data")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        MyARR2 = .Range("A3:D" & LR).Value
        .Range("H:H").NumberFormat = "@"
    End With
    LR = 1
    
    Set wsOUT = ThisWorkbook.Sheets("Desired Result")
    wsOUT.UsedRange.Offset(2).ClearContents
    wsOUT.UsedRange.Offset(, 9).ClearContents
    NR = 3
    
    For i1 = 1 To UBound(MyARR1)
        If c = 0 Then
            c = MyARR1(i1, 1)
            FR = NR
        End If
        If MyARR1(i1, 1) = c Then
            wsOUT.Range("A" & NR).Value = MyARR1(i1, 1)
            wsOUT.Range("B" & NR).Value = MyARR1(i1, 2)
            wsOUT.Range("C" & NR).Value = "'" & MyARR1(i1, 3)
            wsOUT.Range("D" & NR).Value = MyARR1(i1, 4)
            If FR = NR Then
                wsOUT.Range("J" & NR).FormulaR1C1 = "=SUMIF(C3,RC3,C4)"
                wsOUT.Range("K" & NR).FormulaR1C1 = "=SUMIF(C8,RC8,C9)"
                wsOUT.Range("L" & NR).FormulaR1C1 = "=C11-C12"
            End If
            
            NR = NR + 1
        End If
        If i1 = UBound(MyARR1) Then
            GoTo NextSet
        ElseIf MyARR1(i1 + 1, 1) <> c Then
    NextSet:
            For i2 = LR To UBound(MyARR2)
                If MyARR2(i2, 1) = c Then
                    Started = True
                    wsOUT.Range("F" & FR).Value = MyARR2(i2, 1)
                    wsOUT.Range("G" & FR).Value = MyARR2(i2, 2)
                    wsOUT.Range("H" & FR).Value = "'" & MyARR2(i2, 3)
                    wsOUT.Range("I" & FR).Value = MyARR2(i2, 4)
                    If i2 = UBound(MyARR2) Then Exit For
                    FR = FR + 1
                ElseIf Started = True Then
                    Started = False
                    NR = WorksheetFunction.Max(FR, NR) + 1
                    LR = i2
                    c = 0
                    Exit For
                End If
            Next i2
        End If
    Next i1
    
    Application.ScreenUpdating = True
    
    End Sub
    Note, I've removed the blank rows at the top of the source sheets.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-14-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Separate and align rows from two separate sheets based on a cell value.

    That is most impressive! A few questions, forgive me:

    Does this code assume that each Unique ID is represented in both sets of data?

    Also, if I were to replace one of the columns with text, will that affect the functionality? I assume if I wanted to add columns to the data sets, I would just add another:

     wsOUT.Range("E" & NR).Value = MyARR1(i1, 5)
    And then adjust the following to account for the new column:

    MyARR1 = .Range("A3:D" & LR).Value
    Lastly, is there a limit to the amount of rows this will search and match? I assume row counts approaching or exceeding 1000 would take a long time to calculate?

    Thanks again JBeaucaire!

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Separate and align rows from two separate sheets based on a cell value.

    The macro assumes all the codes needed exist in Internal Source Data sheet. The Outside data can have fewer data sets. As long as both sets are sorted the way your samples were, it should work even on larger data sets.

    You are correct about the second parameter in the Arrays being the number of columns. But be careful to adjust the SUMIF formulas as well. Currently they are referencing columns 3,4 and 8,9. Those would change if you added columns or moved things around.

    With that much data, adding the SUMIF formulas as we go actually slows down the macro more than needed. So I've updated it to add them at the very end instead. I lengthened the data set to 1000 rows and it's pretty fast, still.

    Option Explicit
    
    Sub Consolidate()
    Dim wsOUT As Worksheet, NR As Long, FR As Long, i1 As Long, i2 As Long, c As Long, LR As Long
    Dim MyARR1 As Variant, MyARR2 As Variant, Started As Boolean, StartTIME As Double
    
    Application.ScreenUpdating = False
    StartTIME = Now
    
    With ThisWorkbook.Sheets("Internal Source Data")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        MyARR1 = .Range("A3:D" & LR).Value
        .Range("C:C").NumberFormat = "@"
    End With
    With ThisWorkbook.Sheets("Outside Source Data")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        MyARR2 = .Range("A3:D" & LR).Value
        .Range("H:H").NumberFormat = "@"
    End With
    LR = 1
    
    Set wsOUT = ThisWorkbook.Sheets("Desired Result")
    wsOUT.UsedRange.Offset(2).ClearContents
    wsOUT.UsedRange.Offset(, 9).ClearContents
    NR = 3
    
    For i1 = 1 To UBound(MyARR1)
        If c = 0 Then
            c = MyARR1(i1, 1)
            FR = NR
        End If
        If MyARR1(i1, 1) = c Then
            wsOUT.Range("A" & NR).Value = MyARR1(i1, 1)
            wsOUT.Range("B" & NR).Value = MyARR1(i1, 2)
            wsOUT.Range("C" & NR).Value = "'" & MyARR1(i1, 3)
            wsOUT.Range("D" & NR).Value = MyARR1(i1, 4)
            NR = NR + 1
        End If
        If i1 = UBound(MyARR1) Then
            GoTo NextSet
        ElseIf MyARR1(i1 + 1, 1) <> c Then
    NextSet:
            For i2 = LR To UBound(MyARR2)
                If MyARR2(i2, 1) = c Then
                    Started = True
                    wsOUT.Range("F" & FR).Value = MyARR2(i2, 1)
                    wsOUT.Range("G" & FR).Value = MyARR2(i2, 2)
                    wsOUT.Range("H" & FR).Value = "'" & MyARR2(i2, 3)
                    wsOUT.Range("I" & FR).Value = MyARR2(i2, 4)
                    If i2 = UBound(MyARR2) Then Exit For
                    FR = FR + 1
                ElseIf Started = True Then
                    Started = False
                    NR = WorksheetFunction.Max(FR, NR) + 1
                    LR = i2
                    c = 0
                    Exit For
                End If
            Next i2
        End If
    Next i1
    
    With wsOUT.Range("A3:A" & Rows.Count).SpecialCells(xlConstants)
        For i1 = 1 To .Areas.Count
            wsOUT.Range("J" & .Areas(i1).Cells(1).Row).FormulaR1C1 = "=SUMIF(C3,RC3,C4)"
            wsOUT.Range("K" & .Areas(i1).Cells(1).Row).FormulaR1C1 = "=SUMIF(C8,RC8,C9)"
            wsOUT.Range("L" & .Areas(i1).Cells(1).Row).FormulaR1C1 = "=C10-C11"
        Next i1
    End With
    'wsOUT.Range("J:L").Value = wsOUT.Range("J:L").Value
    Application.ScreenUpdating = True
    MsgBox "Done - processing time: " & Format(Now - StartTIME, "h:mm:ss")
    End Sub
    Attached Files Attached Files

+ 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. [SOLVED] Macro needed to Concatenate data in 2 separate tables of 2 separate sheets
    By 823 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 02-08-2015, 02:02 PM
  2. Creating separate sheets based on single rows in list?
    By Taxster in forum Excel General
    Replies: 3
    Last Post: 12-16-2014, 04:24 PM
  3. Replies: 3
    Last Post: 01-03-2014, 02:13 AM
  4. Replies: 2
    Last Post: 07-06-2012, 05:31 AM
  5. Replies: 8
    Last Post: 09-18-2011, 05:36 AM
  6. Replies: 4
    Last Post: 11-22-2010, 12:57 PM
  7. Replies: 3
    Last Post: 08-12-2010, 03:45 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