Combining two tables in Excel using VBA

后端 未结 1 796
梦毁少年i
梦毁少年i 2020-12-22 07:08

Using Excel VBA I would like to be able to combine two tables in excel with a common key. I have suggested ADODB as a method,but am open to any other more efficient/elegant

1条回答
  •  礼貌的吻别
    2020-12-22 07:41

    Depending on whether or not you have duplicate values on either sheet, I could think of a few ideas, not using SQL though.

    • Get LastRow of SourceSheet1 & SourceSheet2 - Set them as variables lastRow1 & lastRow2
    • Create a row ticker for each sheet. s1Row, s2Row, tRow
    • set tRow = 2 For the TargetSheet's first line
    • Use For loop to cycle through each row of SourceSheet1. Using something like this
    • When the first part of code is done looping, you will be finished adding every item from SourceSheet1 onto the TargetSheet. Then you will have to check the values from SourceSheet2 to see if any were unique to that list.
    • When that is done, you should have only added the ones that were missing from your initial search. Then the targetSheet will be in the order of SourceSheet1 All Items, then the extra items from SourceSheet2

    SET VARIABLES

    Private Sub JoinLists()
    
    Dim rng As Range
    Dim typeName As String
    Dim matchCount As Integer
    Dim s1Row As Integer
    Dim s2Row As Integer
    Dim tRow As Integer
    Dim m As Integer
    Dim lastRow1 As Integer
    Dim lastRow2 As Integer
    Dim SourceSheet1 As String
    Dim SourceSheet2 As String
    Dim TargetSheet As String
    
    SourceSheet1 = "Source1"
    SourceSheet2 = "Source2"
    TargetSheet = "Target"
    
    tRow = 2
    
    lastRow1 = Sheets(SourceSheet1).Range("A65536").End(xlUp).row
    lastRow2 = Sheets(SourceSheet2).Range("A65536").End(xlUp).row
    

    PHASE ONE: Copying every entry from Sheet1 to Target, while grabbing matches from Sheet2

    Set rng = Sheets(SourceSheet2).Range("A2:A" & lastRow2)
    
    For s1Row = 2 To lastRow1
        typeName = Sheets(SourceSheet1).Cells(s1Row, 1)
        matchCount = Application.WorksheetFunction.CountIf(rng, typeName)
    
        'Set the Row up on the TargetSheet. No matter if it's a match.
        Sheets(TargetSheet).Cells(tRow, 1) = typeName
        Sheets(TargetSheet).Cells(tRow, 2) = Sheets(SourceSheet1).Cells(s1Row, 2)
        Sheets(TargetSheet).Cells(tRow, 3) = Sheets(SourceSheet1).Cells(s1Row, 3)
    
        'Check to see if there are any matches on SourceSheet2
    
        If matchCount = 0 Then
        'There are NO matches.  Add Zeros to the extra columns
            Sheets(TargetSheet).Cells(tRow, 4) = 0
            Sheets(TargetSheet).Cells(tRow, 5) = 0
        Else
           'Get first matching occurance on the SourceSheet2
            m = Application.WorksheetFunction.Match(typeName, rng, 0)
            'Get Absolute Row number of that match
            s2Row = m + 1    ' This takes into account the Header Row, as index 1 is Row 2 of the search Range
            'Set the extra columns on TargetSheet to the Matches on SourceSheet2
            Sheets(TargetSheet).Cells(tRow, 4) = Sheets(SourceSheet1).Cells(s2Row, 2)
            Sheets(TargetSheet).Cells(tRow, 5) = Sheets(SourceSheet1).Cells(s2Row, 3)
        End If
    
        tRow = tRow + 1
    Next s1Row
    

    PHASE TWO: Checking SourceSheet2 for Entries NOT on Sheet1

    Set rng = Sheets(SourceSheet1).Range("A2:A" & lastRow1)
    
    For s2Row = 2 To lastRow2
        typeName = Sheets(SourceSheet2).Cells(s2Row, 1)
        matchCount = Application.WorksheetFunction.CountIf(rng, typeName)
    
        If matchCount = 0 Then
        'There are NO matches.  Add to Target Sheet
            Sheets(TargetSheet).Cells(tRow, 1) = typeName
            Sheets(TargetSheet).Cells(tRow, 2) = 0
            Sheets(TargetSheet).Cells(tRow, 3) = 0
            Sheets(TargetSheet).Cells(tRow, 4) = Sheets(SourceSheet2).Cells(s2Row, 2)
            Sheets(TargetSheet).Cells(tRow, 5) = Sheets(SourceSheet2).Cells(s2Row, 3)
            tRow = tRow + 1
        'Not doing anything for the matches, because they were already added.
        End If
    Next s2Row
    End Sub
    

    Finished Tested Code Results

    EDIT: typo correction

    0 讨论(0)
提交回复
热议问题