I have a project that I am working on that is copying data from 1 tab and pasting into the other but I am coming upon an obstacle. I have a series of code that is comparing data in column A and if it matches then it pastes the quantity in column F. However if there is a row where the data doesn't match, it messes the rest of the code up. Can someone help me fix this code and recommend what If statement I could add to fix this? I know that it has to do with k not increasing when row 15 doesn't exist in S3 but I'm unsure how to continue.
VBA Code:
Sub Datafinder()
Dim r As Long
Dim j As Long
Dim StoreData As String
Dim S1 As Worksheet
Dim S2 As Worksheet
Dim S3 As Worksheet
Set S1 = Worksheets("2021 DATA")
Set S2 = Worksheets("Template")
Set S3 = Worksheets("2022 DATA")
j = 11
k = 11
For s = 2 To 10000
StoreData1 = S3.Cells(s, "B").Value
If StoreData1 = S2.Range("A2") And S2.Cells(k, "A").Value = S3.Cells(s, "D").Value Then
S3.Activate
S3.Cells(s, "I").Copy
S2.Activate
Cells(k, "F").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
k = k + 1
ElseIf StoreData1 = S2.Range("A2") And S2.Cells(k, "A").Value <> S3.Cells(s, "D").Value Then
S3.Cells(s, "D").Copy
S2.Activate
Cells(j, "A").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "E").Copy
S2.Activate
Cells(j, "B").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "H").Copy
S2.Activate
Cells(j, "C").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "F").Copy
S2.Activate
Cells(j, "D").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "I").Copy
S2.Activate
Cells(j, "F").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
j = j + 1
End If
Next s
End Sub
Data:
VBA Code:
Sub Datafinder()
Dim r As Long
Dim j As Long
Dim StoreData As String
Dim S1 As Worksheet
Dim S2 As Worksheet
Dim S3 As Worksheet
Set S1 = Worksheets("2021 DATA")
Set S2 = Worksheets("Template")
Set S3 = Worksheets("2022 DATA")
j = 11
k = 11
For s = 2 To 10000
StoreData1 = S3.Cells(s, "B").Value
If StoreData1 = S2.Range("A2") And S2.Cells(k, "A").Value = S3.Cells(s, "D").Value Then
S3.Activate
S3.Cells(s, "I").Copy
S2.Activate
Cells(k, "F").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
k = k + 1
ElseIf StoreData1 = S2.Range("A2") And S2.Cells(k, "A").Value <> S3.Cells(s, "D").Value Then
S3.Cells(s, "D").Copy
S2.Activate
Cells(j, "A").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "E").Copy
S2.Activate
Cells(j, "B").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "H").Copy
S2.Activate
Cells(j, "C").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "F").Copy
S2.Activate
Cells(j, "D").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
S3.Cells(s, "I").Copy
S2.Activate
Cells(j, "F").PasteSpecial xlPasteValues
S3.Application.CutCopyMode = False
j = j + 1
End If
Next s
End Sub
Data:
Sku # | Product description | Bag Weight | Case Qty | 2021 units purchased | 2022 units purchased | Should be these #s |
71371 | 71371 | 3 | 16/3 Lb | 16 | 48 | |
71380 | 71380 | 3 | 16/3 Lb | 32 | 16 | |
71390 | 71390 | 3 | 16/3 Lb | 16 | 112 | |
74262 | 74262 | 3 | 16/3 Lb | 32 | 80 | |
7003643 | 7003643 | 25 | 25 Lb | 20 | SKIP | |
7067713 | 7067713 | 3 | 16/3 Lb | 32 | 80 | |
7096241 | 7096241 | 25 | 25 Lb | 16 | SKIP | |
7123920 | 7123920 | 10 | 5/10 Lb | 15 | SKIP | |
7133119 | 7133119 | 5 | 10/5 Lb | 20 | SKIP | |
7133150 | 7133150 | 25 | 25 Lb | 15 | SKIP | |
7280498 | 7280498 | 3 | 10/3.75 Lb | 75 | 30 | |
7280506 | 7280506 | 10 | 5/10 Lb | 40 | 10 | |
7497027 | 7497027 | 7 | 6/7 Lb | 30 | 12 | |
7497043 | 7497043 | 7 | 6/7 Lb | 48 | 42 | |
7497084 | 7497084 | 7 | 6/7 Lb | 18 | 24 | |
7507809 | 7507809 | 15 | 3/15 Lb | 3 | SKIP | |
7638323 | 7638323 | 25 | 25 Lb | 10 | SKIP | |
7794373 | 7794373 | 1 | 15/1 Lb | 15 | 15 | |
7067713 | 7067713 | 3 | 16/3 Lb | 80 | ||
7280498 | 7280498 | 3.75 | 10/3.75 Lb | 30 | ||
7280506 | 7280506 | 10 | 5/10 Lb | 10 | ||
7497027 | 7497027 | 7 | 6/7 Lb | 12 | ||
7497043 | 7497043 | 7 | 6/7 Lb | 42 | ||
7497084 | 7497084 | 7 | 6/7 Lb | 24 | ||
7794373 | 7794373 | 1 | 15/1 Lb | 15 |