Remove Duplicates and Copy Data in Column F

Aragan

New Member
Joined
Mar 27, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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:
Sku #Product descriptionBag WeightCase Qty2021 units purchased2022 units purchasedShould be these #s
7137171371
3​
16/3 Lb1648
7138071380
3​
16/3 Lb3216
7139071390
3​
16/3 Lb16112
7426274262
3​
16/3 Lb3280
70036437003643
25​
25 Lb20SKIP
70677137067713
3​
16/3 Lb3280
70962417096241
25​
25 Lb16SKIP
71239207123920
10​
5/10 Lb15SKIP
71331197133119
5​
10/5 Lb20SKIP
71331507133150
25​
25 Lb15SKIP
72804987280498
3​
10/3.75 Lb7530
72805067280506
10​
5/10 Lb4010
74970277497027
7​
6/7 Lb3012
74970437497043
7​
6/7 Lb4842
74970847497084
7​
6/7 Lb1824
75078097507809
15​
3/15 Lb3SKIP
76383237638323
25​
25 Lb10SKIP
77943737794373
1​
15/1 Lb1515
70677137067713
3​
16/3 Lb
80​
72804987280498
3.75​
10/3.75 Lb
30​
72805067280506
10​
5/10 Lb
10​
74970277497027
7​
6/7 Lb
12​
74970437497043
7​
6/7 Lb
42​
74970847497084
7​
6/7 Lb
24​
77943737794373
1​
15/1 Lb
15​
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Can you please write down which sheets and what to compare verbally? Because, for instance, you have S1 sheet but you never use.
 
Upvote 0

Forum statistics

Threads
1,224,948
Messages
6,181,964
Members
453,077
Latest member
cavsmathur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top