Hi all,
I'm stuck on this one. Trying to pull data from one sheet into a table of another sheet only if the two specific criteria do not exist. Code below.
I'm stuck on this one. Trying to pull data from one sheet into a table of another sheet only if the two specific criteria do not exist. Code below.
VBA Code:
Sub Button3_Click()
Dim wsSource As Worksheet, wsDestination As Worksheet
Dim LastRowSource As Long, LastRowDestination As Long
Dim i As Long, y As Long
Dim value_1 As String, Value_2 As String
Dim ValueExists As Boolean
With ThisWorkbook
Set wsSource = .Worksheets("Data Dump")
Set wsDestination = .Worksheets("YEAR-Test")
End With
With wsSource
'Find the last row of Column A, wsSource
LastRowSource = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop Column A, wsSource
For i = 1 To LastRowSource
'Testing Columns F & G
value_1 = .Range("F" & i).Value
Value_2 = .Range("G" & i).Value
ValueExists = False
With wsDestination
'Find the last row of Column A, wsDestination
LastRowDestination = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop Column A, wsDestination
For y = 1 To LastRowDestination
'For Each value_1 In Table1
If .Range("F" & y).Value = value_1 And .Range("G" & y).Value = Value_2 Then
ValueExists = True
Exit For
GoTo skip
'Else
End If
Next y
'If value does not exist copy
If ValueExists = False Then
.Range("F" & LastRowDestination + 1).Value = value_1
.Range("A" & LastRowDestination + 1).Value = Value_2
.Range("B" & LastRowDestination + 1).Value = wsSource.Range("L" & i).Value
.Range("D" & LastRowDestination + 1).Value = wsSource.Range("R" & i).Value
.Range("E" & LastRowDestination + 1).Value = wsSource.Range("D" & i).Value
.Range("G" & LastRowDestination + 1).Value = wsSource.Range("H" & i).Value
.Range("I" & LastRowDestination + 1).Value = wsSource.Range("C" & i).Value
.Range("J" & LastRowDestination + 1).Value = wsSource.Range("V" & i).Value
.Range("K" & LastRowDestination + 1).Value = wsSource.Range("M" & i).Value
.Range("M" & LastRowDestination + 1).Value = "=VLOOKUP(L5,Defects2!Print_Area,2,FALSE)"
.Range("O" & LastRowDestination + 1).Value = wsSource.Range("N" & i).Value
.Range("R" & LastRowDestination + 1).Value = wsSource.Range("O" & i).Value
.Range("S" & LastRowDestination + 1).Value = wsSource.Range("P" & i).Value
End If
'Next y
End With
skip:
Next i
End With
End Sub