Combine two large tables into one table based on unique ID

后端 未结 4 1059
心在旅途
心在旅途 2020-12-22 06:23

To start, I have little knowledge of VBA and have not tried to write a code for what I want to do as I don\'t even know where to start.

I currently have two tables.

4条回答
  •  难免孤独
    2020-12-22 07:06

    Here's an attempt at getting a sorted and combined table. The general strategy I've employed here is: make copies of existing tables and use them to add values, remove repetitive values, and do the same for the third combined table on sheet 3. Attach the following code to a command button.

    Application.ScreenUpdating = False
    Dim i As Long, x As Long, n As Long, j As Long
    Dim cashtotal As Integer
    
    lastrow1 = Sheet1.Range("A1048575").End(xlUp).Row
    astrow2 = Sheet2.Range("A1048575").End(xlUp).Row
    cashtotal = 0
    x = 1
    
    '''''Routine to make a copy of the existing data.
    For i = 1 To lastrow1
        Sheet1.Cells(i, 4) = Sheet1.Cells(i, 1)
        Sheet1.Cells(i, 5) = Sheet1.Cells(i, 2)
    Next
    
    '''''On Sheet1- Routine to remove repetitive values
    For i = 2 To lastrow1
        If Sheet1.Cells(i, 4) = "" Then GoTo 10
          x = x + 1
          cashtotal = Sheet1.Cells(i, 5)
          Sheet1.Cells(x, 7) = Sheet1.Cells(i, 4)
          Sheet1.Cells(x, 8) = Sheet1.Cells(i, 5)
    
            For j = i + 1 To lastrow1
               If Sheet1.Cells(j, 4) = Sheet1.Cells(i, 4) Then
                 cashtotal = cashtotal + Sheet1.Cells(j, 5)
                 Sheet1.Cells(x, 8) = cashtotal
                 Sheet1.Cells(j, 4).ClearContents
                 Sheet1.Cells(j, 5).ClearContents
               End If
            Next
    10
    Next
    x = 1
    
    '''''On Sheet2 the following routine makes a copy of the existing data
    For i = 1 To lastrow2
        Sheet2.Cells(i, 4) = Sheet2.Cells(i, 1)
        Sheet2.Cells(i, 5) = Sheet2.Cells(i, 2)
    Next
    
    '''''On sheet2 -  Routine to remove repetitive values
    For i = 2 To lastrow2
        If Sheet2.Cells(i, 4) = "" Then GoTo 20
           x = x + 1
           cashtotal = Sheet2.Cells(i, 5)
           Sheet2.Cells(x, 7) = Sheet2.Cells(i, 4)
           Sheet2.Cells(x, 8) = Sheet2.Cells(i, 5)
              For j = i + 1 To lastrow2
                If Sheet2.Cells(j, 4) = Sheet2.Cells(i, 4) Then
                  cashtotal = cashtotal + Sheet2.Cells(j, 5)
                  Sheet2.Cells(x, 8) = cashtotal
                  Sheet2.Cells(j, 4).ClearContents
                  Sheet2.Cells(j, 5).ClearContents
                End If
              Next
    20
    Next
    x = 1
    
    '''Transfer modified tables on sheet1 and sheet2 to sheet3 in a combined table
    lastrow4 = Sheet1.Range("G1048575").End(xlUp).Row
    
    For i = 1 To lastrow4
        Sheet3.Cells(i, 1) = Sheet1.Cells(i, 7)
        Sheet3.Cells(i, 2) = Sheet1.Cells(i, 8)
    Next
    
    lastrow5 = Sheet2.Range("G1048575").End(xlUp).Row
    lastrow6 = Sheet3.Range("A1048575").End(xlUp).Row
    
    For i = 2 To lastrow5
        Sheet3.Cells(lastrow6 + i - 1, 1) = Sheet2.Cells(i, 7)
        Sheet3.Cells(lastrow6 + i - 1, 2) = Sheet2.Cells(i, 8)
    Next
    
    '''''''Routine to make a copy of the existing table
    lastrow7 = Sheet3.Range("A1048575").End(xlUp).Row
    
    For i = 1 To lastrow7
        Sheet3.Cells(i, 4) = Sheet3.Cells(i, 1)
        Sheet3.Cells(i, 5) = Sheet3.Cells(i, 2)
    Next
    
    '''''''' Routine to remove repetitive values
    For i = 2 To lastrow7
        If Sheet3.Cells(i, 4) = "" Then GoTo 30
          x = x + 1
          cashtotal = Sheet3.Cells(i, 5)
          Sheet3.Cells(x, 7) = Sheet3.Cells(i, 4)
          Sheet3.Cells(x, 8) = Sheet3.Cells(i, 5)
             For j = i + 1 To lastrow7
                If Sheet3.Cells(j, 4) = Sheet3.Cells(i, 4) Then
                   cashtotal = cashtotal + Sheet3.Cells(j, 5)
                   Sheet3.Cells(x, 8) = cashtotal
    
                   Sheet3.Cells(j, 4).ClearContents
                   Sheet3.Cells(j, 5).ClearContents
                End If
            Next
    30
    Next
    Application.ScreenUpdating = True
    

提交回复
热议问题