VBA to copy values based on identical text strings in cells and remove duplicates

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. Windows
In Sheet1 (starting from row3, A-X columns) I have a data summary (product's mark and product's prices) and Sheet2 is only for the raw data.

Product's mark is characteristic which contains one to six letters (always to the left of price).

I need a Macro to copy/append data from Sheet2 to Sheet1 in an appropriate column (based on identical products columns). // something like hlookup function in macro
Also, I need to remove (if there are) duplicates based on mark columns (cells which already contains the same marks as marks in Sheet1).

Example.

Sheet1 (before update)

ABCDEF...
1
2
3Product1Product5Product4
4a13b21s11
5b8f16pq31
6c12rd11
7w9

<tbody>
</tbody>

Sheet2 (obtained new "raw" data)

ABCDEF...
1
2
3Product4Product1Product5
4w12b10f18
5d
34c13g
31
6e
28
8............

<tbody>
</tbody>

Sheet1 (after update/append from Sheet2, with removed duplicates/same dates)
ABCDEF...
1
2
3Product1
Product5Product4
4a 13b21s11
5b8f16pq31
6c12g
31
rd11
7e
28
w9
8.........d
34
9....

<tbody>
</tbody>
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this For Update of sheet1 from sheet2.
NB:- Run code from sheet1
Code:
[COLOR="Navy"]Sub[/COLOR] MG20Aug39
[COLOR="Navy"]Dim[/COLOR] cRng [COLOR="Navy"]As[/COLOR] Range, cDn [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rng [COLOR="Navy"]As[/COLOR] Range, Dic [COLOR="Navy"]As[/COLOR] Object, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] cRng2 [COLOR="Navy"]As[/COLOR] Range, cDn2 [COLOR="Navy"]As[/COLOR] Range, Dn2 [COLOR="Navy"]As[/COLOR] Range, Rng2 [COLOR="Navy"]As[/COLOR] Range, temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] cRng = Range(Range("A3"), Cells(3, Columns.Count).End(xlToLeft))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] cDn [COLOR="Navy"]In[/COLOR] cRng
    c = 0
    [COLOR="Navy"]Dim[/COLOR] ray()
     [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
    [COLOR="Navy"]If[/COLOR] Len(cDn.Value) [COLOR="Navy"]Then[/COLOR]
        temp = cDn.Value
         [COLOR="Navy"]Set[/COLOR] Rng = Range(Cells(4, cDn.Column - 1), Cells(Rows.Count, cDn.Column - 1).End(xlUp))
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
                [COLOR="Navy"]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                    c = c + 1
                    ReDim Preserve ray(1 To 2, 1 To c)
                    ray(1, c) = Dn.Value: ray(2, c) = Dn.Offset(, 1)
                    Dic.Add (Dn.Value), Nothing
                [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]Next[/COLOR] Dn
    [COLOR="Navy"]End[/COLOR] If

[COLOR="Navy"]If[/COLOR] Dic.Count <> 0 [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
[COLOR="Navy"]Set[/COLOR] cRng2 = .Range(.Range("A3"), .Cells(3, .Columns.Count).End(xlToLeft))

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] cDn2 [COLOR="Navy"]In[/COLOR] cRng2
    [COLOR="Navy"]If[/COLOR] Len(cDn2.Value) And cDn2.Value = temp [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Rng2 = .Range(.Cells(4, cDn2.Column - 1), .Cells(Rows.Count, cDn2.Column - 1).End(xlUp))
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn2 [COLOR="Navy"]In[/COLOR] Rng2
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Dn2.Value) [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            ReDim Preserve ray(1 To 2, 1 To c)
            ray(1, c) = Dn2.Value: ray(2, c) = Dn2.Offset(, 1)
            Dic.Add (Dn2.Value), Nothing
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] Dn2
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] cDn2
[COLOR="Navy"]With[/COLOR] Cells(4, cDn.Column - 1).Resize(c, 2)
    .Value = Application.Transpose(ray)
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] cDn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,377
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