I have a piece of modified code which I've been using but is very inefficient. The intention was to check if records in 'Database1' sheet exists in 'Log1' if so do nothing if not add the record to first available row. There are multiple iterations of a record in Log1. There should always only be one instance of the record in Database1.
Each time the code runs it replaces all records in Database1.
It seems to be cheking row1 database1 versus row1 Log1 and not the whole range so it copies in multiple entries for one record even though it already exists.
Can anyone help? Apologies if I don't articulate this clearly please ask and I will add more detail if needed.
Option Explicit
Sub Checkrecordthenaddifnotexists()
Dim Ws As Worksheet
Dim i As Long, j As Long
Dim k As Long
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim objTable As ListObject
Application.Calculation = xlCalculationAutomatic
Set sht = Worksheets("Database1")
Sheets("Database1").Select
Cells.Select
sht.Sort.SortFields.Clear
sht.Sort.SortFields.Add Key:=Range("A:A"), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Database1").Sort
.SetRange Range("A:AB")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Log1").Select
Cells.Select
ActiveWorkbook.Worksheets("Log1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Log1").Sort.SortFields.Add Key:=Range("B:B"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Log1").Sort
.SetRange Range("A:AJ")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
sht.Activate
Set StartCell = Range("A2")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
On Error Resume Next
'Sheet2.ShowAllData
Sheet2.Select
Selection.AutoFilter
On Error GoTo 0
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
With ActiveSheet
.ListObjects(1).Name = "Database_v0.1"
End With
Set Ws = Sheets("Database1")
Dim RowsMaster As Integer, Rows2 As Integer
RowsMaster = Ws.Cells(1048576, 1).End(xlUp).Row
Rows2 = Worksheets("Log1").Cells(1048576, 2).End(xlUp).Row
With Worksheets("Log1")
For i = 2 To Rows2
For j = 2 To RowsMaster + 1
If .Cells(i, 1) = Ws.Cells(j, 1) Then
Exit For
End If
Next j
If j = RowsMaster + 1 Then
RowsMaster = RowsMaster + 1
For k = 2 To 8
Ws.Cells(RowsMaster, k - 1) = .Cells(i, k)
Next
End If
Next i
End With
Sheets("Database1").Activate
ActiveSheet.ListObjects("Database_v0.1").Unlist
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("A1:NT1048576").RemoveDuplicates Columns:=1, Header:=xlYes
Sheets("Database Repository").Columns("A").Select
Selection.NumberFormat = "0"
Sheet2.Select
Selection.AutoFilter
Application.Calculation = xlCalculationAutomatic
End Sub