I have an import macro, which creates ID by concatenating cells, then I compare using VLOOKUP with another sheet if any duplicate found.
It's running very slowly, so I want to know better ways to optimize this code, because once finished, I will need to add another "for" for to handle duplicates found and compare dates.
It's one of my first macros in VBA, so I'm sure there are a lot of ways to improve the performance.
Sub ImportData()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim slr As Long
Dim dlr As Long
Dim Tlr As Long
Set wb1 = ActiveWorkbook
FileToOpen = Application.GetOpenFilename _
(Title:="Select import file", _
FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
slr = wb2.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
wb2.Worksheets("Sheet1").Range("A8:S" & slr).Copy _
wb1.Worksheets("INPUT_DATA").Range("A2")
End If
wb2.Close savechanges:=False
dlr = wb1.Worksheets("INPUT_DATA").Cells(Rows.Count, 1).End(xlUp).Row
wb1.Worksheets("INPUT_DATA").Range("A2:S" & dlr).ClearFormats
For cell = 2 To dlr
Cells(cell, 20).Formula = "=CONCAT(RC[-19], ""__"",RC[-18])"
Next
'check duplicate values before import to TOTAL_DATA
Tlr = wb1.Worksheets("TOTAL_DATA").Cells(Rows.Count, 1).End(xlUp).Row
countMatch = 0
countUnmatch = 0
For cell = 2 To dlr
Cells(cell, 21).Formula = "=IF(ISNA(VLOOKUP(RC[-1],TOTAL_DATA!C30,1,FALSE)), ""No"", ""Yes"")"
If Cells(cell, 21).Value = "Yes" Then
Cells(cell, 20).Font.Color = vbRed
countMatch = countMatch + 1
Else
Range("A" & cell, "T" & cell).Cut Destination:=wb1.Worksheets("TOTAL_DATA").Range("A" & Tlr + 1)
Tlr = Tlr + 1
countUnmatch = countUnmatch + 1
End If
Next cell
If countMatch > 0 Then
MsgBox "Found duplicates!!" & vbCr & "Number of duplicates : " & countMatch & _
vbCr & "Duplicate items were keep at INPUT_DATA" & vbCr & _
"Loaded succesfully : " & countUnmatch & " items", vbExclamation
Else
MsgBox "Loaded succesfully : " & countUnmatch & " items"
End If
End Sub
```