can sort data based on replace ?

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,494
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
 
Try:
VBA Code:
Sub MatchData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, arr1 As Variant, arr2 As Variant, lRow As Long
    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
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Range("C2").FormulaR1C1 = "=MID(RC[-2],FIND(""-"",RC[-2])+1,99999)"
                .Range("C2").AutoFill Destination:=.Range("C2:C11"), Type:=xlFillDefault
                .Range("C2:C11").Value = .Range("C2:C11").Value
                .Cells(1, 3).Sort Key1:=.Columns(3), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
                .Columns(3).Delete
                dic.RemoveAll
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
thanks again , but why delete the value column ? I want it . just I forgot issue value column in post#10
your code still doesn't arrange
sheet1
ARRANGE.xlsm
AB
1ITEMBRAND
2AA00-1FOOD/100- TUNE 200G SS
3AA00-2FOOD/101- TUNE 160G SS L/NN
4AA00-3FOOD/101- TUNE 160G SS L/NN1
5AA00-4FOOD/102- TUNE 180G SS
6AA00-5FOOD/103- TU 120G SS
7AA00-6FOOD/104- TUNE 140G SS M
8AA00-7FOOD/104- TUNE 140G SS
9AA00-8FOOD/105- TUNE 200G
10AA00-9FOOD/106- TUNE 200G SS X-V1
11AA00-10FOOD/107- TUNE 200G SS
12AA00-12FOOD/108- TUNE 200G SS DDT
13AA00-22FOOD/109- TUNE 200G S
14AA00-23FOOD/109- TUNE 200G S
15AA00-24FOOD/109- TUNE 200G S
16AA00-25FOOD/109- TUNE 200G S
17AA00-26FOOD/109- TUNE 200G S
18AA00-27FOOD/109- TUNE 200G S
19AA00-28FOOD/109- TUNE 200G S
20AA00-29FOOD/109- TUNE 200G S
21AA00-30FOOD/109- TUNE 200G S
22AA00-31FOOD/109- TUNE 200G S
23AA00-32FOOD/109- TUNE 200G S
24AA00-33FOOD/109- TUNE 200G S
25AA00-34FOOD/109- TUNE 200G S
26AA00-35FOOD/109- TUNE 200G S
27AA00-36FOOD/109- TUNE 200G S
sheet1



sheet2
ARRANGE.xlsm
ABC
1ITEMBRANDQTY
2AA00-1FOOD/100- TUNE 200G SSHGF200
3AA00-2FOOD/101-12 TUNE 160G SS L/NN78
4AA00-4FOOD/102- TUNE ASW 180G SS23
5AA00-5FOOD/103- TU 120G SS200
6AA00-10FOOD/107- TUNE 200G SS BBG120
7AA00-12FOOD/108- TUNE 200G SS DDT S100
8AA00-22FOOD/109- TUNE 200G S45
9AA00-24FOOD/109- TUNE 200G S S45
10AA00-25FOOD/109- TUNE 200G S123
11AA00-33FOOD/109- TUNE 200G S1223
12AA00-7FOOD/104- TUNE 140G SS90
13AA00-8FOOD/105- TUNE 200G 90
14AA00-9FOOD/106- TUNE 200G SS X-V1 ASD90
15AA00-3FOOD/101- TUNE 160G SS L/NN190
sheet2


result based on your code

ARRANGE.xlsm
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-10FOOD/107- TUNE 200G SS
7AA00-12FOOD/108- TUNE 200G SS DDT
8AA00-22FOOD/109- TUNE 200G S
9AA00-24FOOD/109- TUNE 200G S
10AA00-25FOOD/109- TUNE 200G S
11AA00-33FOOD/109- TUNE 200G S
12AA00-7FOOD/104- TUNE 140G SS
13AA00-8FOOD/105- TUNE 200G
14AA00-9FOOD/106- TUNE 200G SS X-V1
15AA00-3FOOD/101- TUNE 160G SS L/NN1
sheet2




what I want
ARRANGE.xlsm
ABC
1ITEMBRANDQTY
2AA00-1FOOD/100- TUNE 200G SS200
3AA00-2FOOD/101- TUNE 160G SS L/NN78
4AA00-3FOOD/101- TUNE 160G SS L/NN190
5AA00-4FOOD/102- TUNE 180G SS23
6AA00-5FOOD/103- TU 120G SS200
7AA00-7FOOD/104- TUNE 140G SS90
8AA00-8FOOD/105- TUNE 200G 90
9AA00-9FOOD/106- TUNE 200G SS X-V190
10AA00-10FOOD/107- TUNE 200G SS120
11AA00-12FOOD/108- TUNE 200G SS DDT100
12AA00-22FOOD/109- TUNE 200G S45
13AA00-24FOOD/109- TUNE 200G S45
14AA00-25FOOD/109- TUNE 200G S123
15AA00-33FOOD/109- TUNE 200G S1223
sheet2

I hope this help
 
Upvote 0
Try:
VBA Code:
Sub MatchData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, arr1 As Variant, arr2 As Variant, lRow As Long
    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
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Range("D2").FormulaR1C1 = "=MID(RC[-3],FIND(""-"",RC[-3])+1,99999)"
                .Range("D2").AutoFill Destination:=.Range("D2:D" & lRow), Type:=xlFillDefault
                .Range("D2:D" & lRow).Value = .Range("D2:D" & lRow).Value
                .Cells(1, 4).Sort Key1:=.Columns(4), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
                .Columns(4).Delete
                dic.RemoveAll
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
excellent ! this fixes the arranging problem .
much appreciated !
 
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,652
Members
452,664
Latest member
alpserbetli

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