modified code arrange data for multiple sheets into multiple files

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
hi
I would modify this code from this thread can sort data based on replace ?
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
so I have many files contains many sheets . the data in files are not arranged for each sheet . so I want arrange the data for each sheet for each file based on another file
the result should be into files pur1,pur2 based on file search . when arrange data should match the column A across multiple sheets for the files with file SEARCH and arrange again based on file SEARCH
file PUR1
PUR1 .xlsx
ABC
1ITEMIDQTY
2BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/10012.000
3BS-TA-117QQW-181 RRSDF BRI234.000
4BS-TA-118QQW-19 CLA19 ITY CV400.000
5BS-TA-119QQW-20 KV/1**2 CLA20 CV123.000
6BS-TA-107QQW-8 CLA8 UKI456.000
7BS-TA-108QQW-9 CLA91 N BR600.000
REPORT


file PUR2
PUR2.xlsx
ABC
1ITEMIDQTY
2BS-TA-106QQW-7 S** CLA7 US BN200.000
3BS-TA-107QQW-8 CLA8 UK ASD120.000
4BS-TA-103QQW-41 M*12.5 CLA4 TR GF100.000
5BS-TA-104QQW-5 CLA5 EG CV NBH-1120.000
6BS-TA-105QQW-612 M230 TU501.000
DATA


file search
SEARCH.xlsm
AB
1ITEMID
2BS-TA-100QQW-1 MM CLA1 23M-1 IT
3BS-TA-101QQW-2 TH NM-1 CLA2 VBG L CHI
4BS-TA-102QQW-3 CV CLA3 TA
5BS-TA-103QQW-4 M*12.5 CLA4 TR
6BS-TA-104QQW-5 CLA5 EG
7BS-TA-105QQW-6 M230 TU
8BS-TA-106QQW-7 S** CLA7 US
9BS-TA-107QQW-8 CLA8 UK
10BS-TA-108QQW-9 CLA9 N BR
11BS-TA-109QQW-10 BN CLA10 IT
12BS-TA-110QQW-11 LVD CH
13BS-TA-111QQW-12 CLA12 JA
14BS-TA-112QQW-13 CLA13 TR
15BS-TA-113QQW-14 L/R CLA14 SS230 EG
16BS-TA-114QQW-15 CLA15 TU
17BS-TA-115QQW-16 CLA16 US
18BS-TA-116QQW-17 CLA17 UK
19BS-TA-117QQW-18 RRSDF BR
20BS-TA-118QQW-19 CLA19 IT
21BS-TA-119QQW-20 KV/1**2 CLA20 CHI
22
RP



result


file pure1
PUR1 .xlsx
ABC
1ITEMIDQTY
2BS-TA-107QQW-8 CLA8 UK456.000
3BS-TA-108QQW-9 CLA9 N BR600.000
4BS-TA-113QQW-14 L/R CLA14 SS230 EG12.000
5BS-TA-117QQW-18 RRSDF BR234.000
6BS-TA-118QQW-19 CLA19 IT400.000
7BS-TA-119QQW-20 KV/1**2 CLA20 CHI123.000
REPORT


file pur2
PUR2.xlsx
ABC
2BS-TA-103QQW-4 M*12.5 CLA4 TR100.000
3BS-TA-104QQW-5 CLA5 EG120.000
4BS-TA-105QQW-6 M230 TU501.000
5BS-TA-106QQW-7 S** CLA7 US200.000
6BS-TA-107QQW-8 CLA8 UK120.000
DATA

with considering I have many sheets for each file
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Place this macro in the SEARCH workbook. Change the sheet name (in blue) and the folder path where the files are saved (in red) to suit your needs.
Rich (BB code):
Sub MatchData()
    Application.ScreenUpdating = False
    Dim desWB As Workbook, 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 = ThisWorkbook.Sheets("RP")
    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
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set desWB = Workbooks.Open(strPath & strExtension)
        For Each ws In Sheets
            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
        Next ws
        With ActiveWorkbook
            .Save
            .Close False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
great code. just question if you have free time. the code works for multiple sheets for each file . I would another choice by making the code for one sheet for each file

each file just contains sheet1 , how can I mod the code, please ?
 
Upvote 0
The code should work without any changes as long as each file contains only one sheet so you can use it for any number of sheets including just one.
 
Upvote 0
thanks very much for your following my thread and provide me big favor :love:
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,153
Members
452,615
Latest member
bogeys2birdies

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