Joining excel records based on column data

前端 未结 2 1189
不思量自难忘°
不思量自难忘° 2021-01-15 21:22

I have 3 excel spreadsheets with the same columns and (supposedly) the same data. I need to line up all 3 documents and look for inconsistencies within the data. The data is

相关标签:
2条回答
  • 2021-01-15 21:45

    Using macros, you can do it.

    Sub Insert_Rows_Loop()
      Dim CurrentSheet As Object
      ' Loop through all selected sheets.
      For Each CurrentSheet In ActiveWindow.SelectedSheets
        ' Insert n rows depending on values
        //  **** 
        // Write your conditions 
        //  ****
        // Insert a row as below
        CurrentSheet.Range("b:b").EntireRow.Insert
        // Do other stuffs....
      Next CurrentSheet
    End Sub
    
    0 讨论(0)
  • 2021-01-15 21:50

    Here's an ADO based solution using Excel and VBA. If you need more info about using ADO with Excel/VBA then try http://support.microsoft.com/kb/257819

    I'm going to assume that all three of your spreadsheets are in a single workbook and that they are named Sheet1, Sheet2 and Sheet3. Obviously adjust the code as necessary.

    To use ADO, go to the Visual Basic Editor (via the menus or via Alt-F11) and then via Tools > References (or its equivalent in later versions) add a reference to: "Microsoft ActiveX Data Objects 2.8 Library"

    I'm using the provider and connection string for Excel 2003 and earlier because that's the version I have. For Excel 2007 and later, use these instead (replacing the filename of course):

    Provider=Microsoft.ACE.OLEDB.12.0

    Data Source=filename;Extended Properties=Excel 12.0 Xml;

    (NB you'll often see "HDR=Yes" in connection strings but this is the default setting for Excel and thus can be omitted)

    Firstly we'll create a master list of asset tags from the original three spreadsheets. Create a blank sheet and call it Sheet4 so that we have somewhere to put the list.

    In a normal module, add the following then run it:

    Sub master_list()
    
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=Excel 8.0;"
        .Open
    End With
    
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    
    rs.Open "SELECT [Asset Tag] FROM [Sheet1$] UNION SELECT [Asset Tag] FROM [Sheet2$] UNION SELECT [Asset Tag] FROM [Sheet3$];", cn
    
    With Worksheets("Sheet4")
        .Cells(1, 1).Value = "Master"
        .Cells(2, 1).CopyFromRecordset rs
    End With
    
    rs.Close
    cn.Close
    
    End Sub
    

    The UNION operator in SQL only returns distinct records so this query has given us a complete list of the asset tags in all three spreadsheets with no duplicates. I've used "Master" as the column name to prevent any ambiguity later on

    Now we need to combine the data from the three individual sheets with the master list. Create a new blank sheet and call that Sheet5. Now add and run the following:

    Sub compare_sheets()
    
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
            "Extended Properties=Excel 8.0;"
        .Open
    End With
    
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    
    rs.Open "SELECT * FROM (([Sheet4$] LEFT JOIN [Sheet1$] ON [Sheet4$].[Master] = [Sheet1$].[Asset Tag]) " & _
        "LEFT JOIN [Sheet2$] ON [Sheet4$].[Master] = [Sheet2$].[Asset Tag]) " & _
        "LEFT JOIN [Sheet3$] ON [Sheet4$].[Master] = [Sheet3$].[Asset Tag];", cn
    
    Dim fld As ADODB.Field
    Dim i As Integer
    i = 0
    With Worksheets("Sheet5")
        For Each fld In rs.Fields
            i = i + 1
            .Cells(1, i).Value = fld.Name
        Next fld
    
        .Cells(2, 1).CopyFromRecordset rs
    End With
    
    rs.Close
    cn.Close
    
    End Sub
    

    That should hopefully give you the data from all three spreadsheets presented against a master list of asset tags. The column names will probably be a bit odd-looking (e.g. "Sheet1$.Asset Tag" etc) and all of the formatting will be lost but at least you can see which sheets are missing data

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