can sort data based on replace ?

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
need macro to implement for about 1000 rows. the idea should match column A between two sheets sheet1,2 .if the data in column B into sheet2 are not matched with sheet1 then should replace data into column B for sheet2 based on sheet1 and arrange as the same thing in sheet1
sheet1
11.xlsx
AB
1ITEMBRAND
2AA00-1FOOD/100- TUNE 200G SS
3AA00-2FOOD/101- TUNE 160G SS L/NN
4AA00-3FOOD/102- TUNE 180G SS
5AA00-4FOOD/103- TU 120G SS
6AA00-5FOOD/104- TUNE 140G SS
7AA00-6FOOD/105- TUNE 200G
8AA00-7FOOD/106- TUNE 200G SS X-V1
9AA00-8FOOD/107- TUNE 200G SS
10AA00-9FOOD/108- TUNE 200G SS DDT
11AA00-10FOOD/109- TUNE 200G S
12
SHEET1


sheet2
11.xlsx
ABC
1ITEMBRANDQTY
2AA00-1FOOD/100- TUNE 200G N-G200
3AA00-6FOOD/100- TUNE 2010G SS123
4AA00-7FOOD/100- TUNE1 200G SS1223
5AA00-8FOOD/100- TU 200G SS90
6AA00-2FOOD/100- TU 200GBS78
7AA00-3FOOD/100- TUNE 200G SS23
8AA00-4FOOD/100- TUNE12 200G SS45
9AA00-5FOOD/100- TUNE 200G SS45
sheet2

result in sheet2
11.xlsx
ABC
1ITEMBRANDQTY
2AA00-1FOOD/100- TUNE 200G SS200
3AA00-2FOOD/101- TUNE 160G SS L/NN78
4AA00-3FOOD/102- TUNE 180G SS23
5AA00-4FOOD/103- TU 120G SS45
6AA00-5FOOD/104- TUNE 140G SS45
7AA00-6FOOD/105- TUNE 200G 123
8AA00-7FOOD/106- TUNE 200G SS X-V11223
9AA00-8FOOD/107- TUNE 200G SS90
sheet2

thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try:
VBA Code:
Sub MatchData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, arr1 As Variant, arr2 As Variant
    Dim dic As Object, srcRng As Range, x As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    Set srcRng = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp))
    arr1 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    arr2 = desWS.Range("A2", desWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(arr1, 1)
        If Not dic.Exists(arr1(i, 1)) Then
            dic.Add arr1(i, 1), Nothing
        End If
    Next i
    For i = 1 To UBound(arr2, 1)
        If dic.Exists(arr2(i, 1)) Then
            If Not IsError(Application.Match(arr2(i, 1), srcRng, 0)) Then
                x = Application.Match(arr2(i, 1), srcRng, 0)
                desWS.Range("B" & i + 1).Value = srcWS.Range("B" & x + 1).Value
            End If
        End If
    Next i
    desWS.Cells(1, 1).Sort Key1:=desWS.Columns(1), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
wow ! this is magnificent ! just question how can I implement your code for many sheets with the same structure ,please?
I have many sheets(sheet2,sheet3,sheet4)
 
Upvote 0
Try:
VBA Code:
Sub MatchData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, arr1 As Variant, arr2 As Variant
    Dim dic As Object, srcRng As Range, x As Long, ws As Worksheet
    Set srcWS = Sheets("Sheet1")
    With srcWS
        arr1 = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 2).Value
        Set srcRng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
    End With
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            With ws
                arr2 = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 2).Value
                Set dic = CreateObject("Scripting.Dictionary")
                For i = 1 To UBound(arr1, 1)
                    If Not dic.Exists(arr1(i, 1)) Then
                        dic.Add arr1(i, 1), Nothing
                    End If
                Next i
                For i = 1 To UBound(arr2, 1)
                    If dic.Exists(arr2(i, 1)) Then
                        If Not IsError(Application.Match(arr2(i, 1), srcRng, 0)) Then
                            x = Application.Match(arr2(i, 1), srcRng, 0)
                            ws.Range("B" & i + 1).Value = srcWS.Range("B" & x + 1).Value
                        End If
                    End If
                Next i
                ws.Cells(1, 1).Sort Key1:=ws.Columns(1), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
            End With
            dic.RemoveAll
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
You are very welcome. :)
 
Upvote 0
sorry I came back , but I've found problem about when match column A . it doesn't seem arrange correctly based on sheet1
it seem filter based on number in column A .it should arrange based on sheet1
orginal in sheet1
11.xlsx
A
1ITEM
2AA00-1
3AA00-2
4AA00-3
5AA00-4
6AA00-5
7AA00-6
8AA00-7
9AA00-8
10AA00-9
11AA00-10
12AA00-12
13AA00-22
sheet1

based on your code it give this
11.xlsx
A
1ITEM
2AA00-1
3AA00-10
4AA00-12
5AA00-2
6AA00-22
7AA00-4
8AA00-5
9AA00-7
10AA00-8
11AA00-9
sheet2


but what I want how is arranged in sheet1 should be the same thing in sheet2 or others sheets
11.xlsx
A
1ITEM
2AA00-1
3AA00-2
4AA00-4
5AA00-5
6AA00-7
7AA00-8
8AA00-9
9AA00-10
10AA00-12
11AA00-22
sheet1
 
Upvote 0
The macro is working properly for me. This is what I get:
Book2
ABC
1ITEMBRANDQTY
2AA00-1FOOD/100- TUNE 200G SS200
3AA00-2FOOD/101- TUNE 160G SS L/NN78
4AA00-3FOOD/102- TUNE 180G SS23
5AA00-4FOOD/103- TU 120G SS45
6AA00-5FOOD/104- TUNE 140G SS45
7AA00-6FOOD/105- TUNE 200G 123
8AA00-7FOOD/106- TUNE 200G SS X-V11223
9AA00-8FOOD/107- TUNE 200G SS90
Sheet2
 
Upvote 0
I'm still gussing your code filter based on the number not based on arranging into sheet1.
please test this data and inform me what happen for you.
sheet1
(version 1).xlsb
AB
1ITEMBRAND
2AA00-1FOOD/100- TUNE 200G SS
3AA00-2FOOD/101- TUNE 160G SS L/NN
4AA00-4FOOD/102- TUNE 180G SS
5AA00-5FOOD/103- TU 120G SS
6AA00-7FOOD/104- TUNE 140G SS
7AA00-8FOOD/105- TUNE 200G
8AA00-9FOOD/106- TUNE 200G SS X-V1
9AA00-10FOOD/107- TUNE 200G SS
10AA00-12FOOD/108- TUNE 200G SS DDT
11AA00-22FOOD/109- TUNE 200G S
sheet1

sheet2
(version 1).xlsb
AB
1ITEMBRAND
2AA00-1FOOD/100- TUNE 200G N-G
3AA00-10FOOD/100- TUNE 2010G SS
4AA00-12FOOD/100- TUNE1 200G SS
5AA00-2FOOD/100- TU 200G SS
6AA00-22FOOD/100- TU 200GBS
7AA00-4FOOD/100- TUNE 200G SS
8AA00-5FOOD/100- TUNE12 200G SS
9AA00-7FOOD/100- TUNE 200G SS
10AA00-8FOOD/105- TUNE 200G MM
11AA00-9FOOD/106- TUNE 200G SS X-V1 ZX
sheet2


this is what I got
(version 1).xlsb
AB
1ITEMBRAND
2AA00-1FOOD/100- TUNE 200G SS
3AA00-10FOOD/107- TUNE 200G SS
4AA00-12FOOD/108- TUNE 200G SS DDT
5AA00-2FOOD/101- TUNE 160G SS L/NN
6AA00-22FOOD/109- TUNE 200G S
7AA00-4FOOD/102- TUNE 180G SS
8AA00-5FOOD/103- TU 120G SS
9AA00-7FOOD/104- TUNE 140G SS
10AA00-8FOOD/105- TUNE 200G
11AA00-9FOOD/106- TUNE 200G SS X-V1
sheet2
 
Upvote 0
sorry I forgot column value in the picture . just see the two columns A,B and you will note it
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,267
Members
449,219
Latest member
daynle

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