'Compare two lists and update missing data through Excel VBA

I have been struggling with the below Excel VBA task. Within the same workbook there are two worksheets "Main" and "Data". The information in "Data" comes from and external source and "Main" contains individual comment data that needs to stay on same row as the corresponding ID.

The task here is to run a macro (VBA) that compares the IDs in "Main" to the ones within the same first column in "Data". If any IDs are missing it shall copy them from "Data" to the first empty row in "Main" and sort by "Main" ensuring the comments in next cell don't deviate from the corresponding ID. Attached are screenshot examples:

Main Tab Data Tab (compare and copy missing IDs from it)



Solution 1:[1]

Good day colleagues, Thank you for your time and suggestions. I managed to troubleshoot the VBA and will post it here for the benefit of anyone that needs to complete a similar task. Best regards to all and stay positive.`Sub Compare()

'Set Ranges
With Worksheets("Data")
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
compar = .Range(.Cells(1, 1), .Cells(lastrow, 1))
End With
Worksheets("Main").Select
lastdata = Cells(Rows.Count, "A").End(xlUp).Row
datar = Range(Cells(1, 1), Cells(lastdata, 1))
indi = lastdata + 1

'Logical Test
 For j = 1 To lastrow
  For i = 1 To lastdata
   fnd = False
   If datar(i, 1) = compar(j, 1) Then
    ' When Found
     fnd = True
     Exit For
   End If
  Next i
  If Not (fnd) Then
      For kk = 1 To 1
       Cells(indi, kk) = compar(j, kk)
      Next kk
      indi = indi + 1
  End If
 Next j

'Sort Result
ActiveWorkbook.Worksheets("Main").ListObjects("Table1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Main").ListObjects("Table1").Sort.SortFields.Add2 _
        Key:=Range("Table1[[#All],[ID]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Main").ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWorkbook.Save
    
End Sub  `

Solution 2:[2]

This is a guide not an answer

Try using code like this;

MyListofNewValues = ""
for each TestRow in Sheets("Data").UsedRange.Rows
   .... get a value to test
   for each CheckRow in Sheets("Main").UsedRange.Rows
      .... Check a value here with the value above
      .... IF Different (ie New) add it to MyListOfNewValues
   next CheckRow
Next TestRow

' Now you have a list of values in Data but Not in Main
' You'll have to add those to main & sort

Comeback when you've tried something showing the code you've tried - or post the code you've already tried

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ipivanov1
Solution 2 Tin Bum