smide
Board Regular
- Joined
- Dec 20, 2015
- Messages
- 164
- Office Version
- 2016
- Platform
- Windows
Hello.
The following code allows me to copy data from Sheet2 into Sheet1 and at the same time to remove duplicates based on the same text in columns A,C,E...
So, for example, if macro finds that the text in cell C9 (Sheet2) is the same as text in any of the cells in column C in Sheet1 he doesn't copy/append cell C9 from Sheet2 but he also skip corresponding/next cell - cell D9 in this example.
So code if fine, but unfortunately it's static, and I want to add product names in row3 in Sheet1 at the top of columns B,D,F...(and also in Sheet2 row3 of course) and when I do this append/remove duplicates I would also like
that somehow code recognize position of the newly added text/strings in cells B3,D3,F3... then to compare them with text in Sheet1 (also row3) and to copy values in the appropriate columns based on the same text. (something like hlookup function in macro)
Example.
Sheet1 (data summary)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product5[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]06.07[/TD]
[TD="align: center"]13[/TD]
[TD="align: center"]05.07[/TD]
[TD="align: center"]21[/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]16[/TD]
[TD="align: center"]...[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]...[/TD]
[/TR]
</tbody>[/TABLE]
Sheet2 (obtained new "raw" data)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product5[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product1[/TD]
[TD="align: center"]........[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]15[/TD]
[TD="align: center"]06.07[/TD]
[TD="align: center"]13[/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]10.07[/TD]
[TD="align: center"]31[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]34[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]11.07[/TD]
[TD="align: center"]46[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: cms_table_grid, align: center"]
<tbody>[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
</tbody>[/TABLE]
Sheet1 (after update/append from Sheet2, with removed duplicates/same dates)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD][/TD]
[TD] A
[/TD]
[TD] B
[/TD]
[TD] C
[/TD]
[TD] D
[/TD]
[TD] E
[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]Product1
[/TD]
[TD][/TD]
[TD]Product5
[/TD]
[TD].....[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]06.07[/TD]
[TD="align: center"] 13[/TD]
[TD]05.07[/TD]
[TD="align: center"]21[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]08.07[/TD]
[TD="align: center"]8[/TD]
[TD]08.07[/TD]
[TD="align: center"]16[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]09.07[/TD]
[TD="align: center"]12[/TD]
[TD]09.07
[/TD]
[TD="align: center"]15
[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]11.07
[/TD]
[TD="align: center"]46
[/TD]
[TD]10.07
[/TD]
[TD="align: center"]31
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]...[/TD]
[TD]....[/TD]
[TD]...[/TD]
[TD]...[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The following code allows me to copy data from Sheet2 into Sheet1 and at the same time to remove duplicates based on the same text in columns A,C,E...
So, for example, if macro finds that the text in cell C9 (Sheet2) is the same as text in any of the cells in column C in Sheet1 he doesn't copy/append cell C9 from Sheet2 but he also skip corresponding/next cell - cell D9 in this example.
Code:
Sub RemoveDupes()
'Columns A and B
Dim w1 As Worksheet, w2 As Worksheet
Dim rng1 As Range, rng2 As Range, c As Range
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
Set rng1 = w1.Range(w1.Range("A3"), w1.Range("A" & Rows.Count).End(xlUp))
Set rng2 = w2.Range(w2.Range("A3"), w2.Range("A" & Rows.Count).End(xlUp))
With CreateObject("Scripting.Dictionary")
.CompareMode = vbTextCompare
For Each c In rng1
If Not .Exists(c.Value) Then
.Add c.Value, c.Offset(, 1)
End If
Next
For Each c In rng2
If Not .Exists(c.Value) Then
.Add c.Value, c.Offset(, 1)
End If
Next
w1.Range("A3").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .Items))
End With
w1.Activate
'Columns C and D
Dim w3 As Worksheet, w4 As Worksheet
Dim rng3 As Range, rng4 As Range, d As Range
Set w3 = Sheets("Sheet1")
Set w4 = Sheets("Sheet2")
Set rng3 = w3.Range(w3.Range("C3"), w3.Range("C" & Rows.Count).End(xlUp))
Set rng4 = w4.Range(w4.Range("C3"), w4.Range("C" & Rows.Count).End(xlUp))
With CreateObject("Scripting.Dictionary")
.CompareMode = vbTextCompare
For Each d In rng3
If Not .Exists(d.Value) Then
.Add d.Value, d.Offset(, 1)
End If
Next
For Each d In rng4
If Not .Exists(d.Value) Then
.Add d.Value, d.Offset(, 1)
End If
Next
w3.Range("C3").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .Items))
End With
w3.Activate
....
So code if fine, but unfortunately it's static, and I want to add product names in row3 in Sheet1 at the top of columns B,D,F...(and also in Sheet2 row3 of course) and when I do this append/remove duplicates I would also like
that somehow code recognize position of the newly added text/strings in cells B3,D3,F3... then to compare them with text in Sheet1 (also row3) and to copy values in the appropriate columns based on the same text. (something like hlookup function in macro)
Example.
Sheet1 (data summary)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product5[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]06.07[/TD]
[TD="align: center"]13[/TD]
[TD="align: center"]05.07[/TD]
[TD="align: center"]21[/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]16[/TD]
[TD="align: center"]...[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]...[/TD]
[/TR]
</tbody>[/TABLE]
Sheet2 (obtained new "raw" data)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD="align: center"][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product5[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Product1[/TD]
[TD="align: center"]........[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]15[/TD]
[TD="align: center"]06.07[/TD]
[TD="align: center"]13[/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]10.07[/TD]
[TD="align: center"]31[/TD]
[TD="align: center"]08.07[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]09.07[/TD]
[TD="align: center"]34[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]11.07[/TD]
[TD="align: center"]46[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: cms_table_grid, align: center"]
<tbody>[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
</tbody>[/TABLE]
Sheet1 (after update/append from Sheet2, with removed duplicates/same dates)
[TABLE="class: grid, align: center"]
<tbody>[TR]
[TD][/TD]
[TD] A
[/TD]
[TD] B
[/TD]
[TD] C
[/TD]
[TD] D
[/TD]
[TD] E
[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]Product1
[/TD]
[TD][/TD]
[TD]Product5
[/TD]
[TD].....[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]06.07[/TD]
[TD="align: center"] 13[/TD]
[TD]05.07[/TD]
[TD="align: center"]21[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]08.07[/TD]
[TD="align: center"]8[/TD]
[TD]08.07[/TD]
[TD="align: center"]16[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]09.07[/TD]
[TD="align: center"]12[/TD]
[TD]09.07
[/TD]
[TD="align: center"]15
[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]11.07
[/TD]
[TD="align: center"]46
[/TD]
[TD]10.07
[/TD]
[TD="align: center"]31
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]...[/TD]
[TD]....[/TD]
[TD]...[/TD]
[TD]...[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Last edited: