Thom Herold
Thom Herold

Reputation: 11

Adding unique data from Source Worksheet to Master Worksheet

A row from the Source List Worksheet (SLW) columns (1, 2 & 3) needs pasted into the Master List Worksheet (MLW) columns (3, 4 & 5) [same order] if the unique ID number (SLW1 = MLW3) does NOT already exists in the "Master List" (same workbook). My first Excel VBA project ever. So any and all advice/suggestions/corrections/short cuts would be great. This code is what I have fumbled creating. As you know, its not working.

Sub Transfer()

    Dim SLR As Integer 'SourceList's Woksheets Last Row
    Dim MLR As Integer 'MasterList's Woksheets Last Row
    Dim SC As Integer 'SourceList Counting through the loop (ROW NUMBER)
    Dim SR As Range 'SourceList A-C Row data
                    '(Source information 3 rows to be transfered)
    Dim ID As Integer 'Unique code of Projects
    Dim Found As Range

    Sheets("SourceList").Activate
    SLR = Cells(Rows.Count, "A").End(xlUp).Row

    'Start loop to go through SourceList unique ID numbers
    For SC = 2 To SLR
        'Copy SourceList ID number into Variable "ID"
        ID = Sheets("SourceList").Range(1, SC)

        'Also, Save Range into Variable so it doesn't have to
        'go back and forth between Worksheets
        Set SR = Range(Cells(1, SC), Cells(3, SC))

        Sheets("MasterList").Activate
        Found = Columns("C:C").Find(What:=ID, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        If Found Is Nothing Then
            MLR = Cells(Rows.Count, "C").End(xlUp).Row + 1
            Range(Cells(3, MLR)) = SR
            SR.ClearContents
        End If
        Sheets("SourceList").Activate
    Next SC
End Sub

Upvotes: 1

Views: 90

Answers (1)

L42
L42

Reputation: 19727

Although I've posted a link for you to check out, I will post this solution which I've used before.

Sub ject()
    Dim con As Object: Set con = CreateObject("ADODB.Connection")
    Dim rec As Object: Set rec = CreateObject("ADODB.Recordset")
    
    Dim datasource As String
    datasource = ThisWorkbook.FullName ' returns the fullpath

    Dim sconnect As String
    sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                "Data Source=" & datasource & ";" & _
                "Extended Properties=""Excel 12.0;HDR=YES"";"
    con.Open sconnect
    
    Dim sqlstr As String
    ' This basically executes anti-join if you know SQL
    sqlstr = "SELECT * "
    sqlstr = sqlstr & "FROM [SWL$] e "
    sqlstr = sqlstr & "LEFT JOIN [MWL$] u "
    sqlstr = sqlstr & "ON e.ID = u.ID "
    sqlstr = sqlstr & "WHERE u.ID IS NULL "
    sqlstr = sqlstr & "AND e.ID IS NOT NULL;"

    rec.Open sqlstr, con, 3, 1
    
    ' Dump data that meets your requirement
    With Sheets("MWL")
        Dim lr As Long
        lr = .Range("D" & .Rows.Count).End(xlUp).Row + 1
        .Range("D" & lr).CopyFromRecordset rec
    End With
End Sub

Considerations:

  1. Your SWL and MWL sheet data should start at Row 1 with headers. enter image description here
  2. Both should have the header name ID which contains the unique identifier. If not, you can adjust the code above.

So what the code does is access ADO (Active Data Objects) to be able to execute data comparison using SQL commands. It is way faster than the conventional Range to Range comparison (looping). I'm not sure if it is faster than Array to Array comparison but it is certainly easier to read and adjust once you get the hang of it. Anyways, this maybe a little bit too much at the moment (since you said it is your first project), but this is tried and tested and certainly works.

IMPORTANT: Notice the sconnect variable. You need to use the correct Connection String depending on the version of your Excel.

Upvotes: 1

Related Questions