VBA - need help to add another feature into existing code

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. 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.

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)

ABCDE
1
2
3Product1Product5
406.071305.0721....
508.07808.0716...
609.0712....
7...

<tbody>
</tbody>

Sheet2 (obtained new "raw" data)

ABCDEF
1
2
3Product5Product1........
409.071506.0713....
510.073108.0712
609.0734
711.0746
8

<tbody>
</tbody>

<tbody>
</tbody>


Sheet1 (after update/append from Sheet2, with removed duplicates/same dates)

A
B
C
D
E
1
2
3Product1
Product5
.....
406.07 1305.0721
508.07808.0716
609.071209.07
15
711.07
46
10.07
31
8.............

<tbody>
</tbody>
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,162
Messages
6,123,382
Members
449,097
Latest member
Jabe

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