combined the same sheet name between two files & save as xlsx

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
Hi experts,
I search for macro to combine the same sheet name between two files are in the same folder . so should combine based on match column B and summing values for columns C,D and insert column BALANCE in column E to subtract column C from column D and save the file as xlsx and the file name should be "FINAL STOCK" and sheet name should be "STOCK" based on sheet name into two files .
the sheet STOCK into two files is existed in last sheet .
STOCK.xlsx
ABCD
1ITEMBRANDIMPORTEXPORT
21ELEC-1000200
32ELEC-10011200
43ELEC-1002122
54ELEC-10031200
65ELEC-1004120
76ELEC-1005300
87ELEC-1006230
98ELEC-1007123
109ELEC-1008123
1110ELEC-1009123
1211ELEC-10101122
1312ELEC-101112210
1413ELEC-10124
1514ELEC-1013123
1615ELEC-10141231
1716ELEC-101511
1817ELEC-101611
1918ELEC-101712
2019ELEC-101812
2120ELEC-10191012
stock



STO.xlsx
ABCD
1ITEMBRANDIMPORTEXPORT
21ELEC-1012123
32ELEC-10131010
43ELEC-10222012
54ELEC-100911
65ELEC-101010
76ELEC-101112
87ELEC-1003220
98ELEC-10044
109ELEC-1005100
1110ELEC-100610
1211ELEC-100710
1312ELEC-100810
1413ELEC-100010
1514ELEC-100110
1615ELEC-100222
1716ELEC-1014112
1817ELEC-1015112
1918ELEC-1016120
2019ELEC-1017102
stock



result (should be the same formatting & borders)
FINAL STOCK.xlsx
ABCDE
1ITEMBRANDIMPORTEXPORTBALANCE
21ELEC-1000210.00- 210.00
32ELEC-10011,210.00- 1,210.00
43ELEC-1002- 144.00-144.00
54ELEC-10031,420.00- 1,420.00
65ELEC-1004120.004.00116.00
76ELEC-1005400.00- 400.00
87ELEC-1006240.00- 240.00
98ELEC-1007133.00- 133.00
109ELEC-1008133.00- 133.00
1110ELEC-1009134.00- 134.00
1211ELEC-10101,132.00- 1,132.00
1312ELEC-1011134.0010.00124.00
1413ELEC-1012123.004.00119.00
1514ELEC-1013133.0010.00123.00
1615ELEC-1014134.003.00131.00
1716ELEC-1015123.00- 123.00
1817ELEC-1016131.00- 131.00
1918ELEC-101722.002.0020.00
2019ELEC-101812.00- 12.00
2120ELEC-101910.0012.00-2.00
2221ELEC-102220.0012.008.00
STOCK

thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try:
VBA Code:
Sub CombineData()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet, wb1 As Workbook, wb2 As Workbook, desWB As Workbook, desWS As Worksheet
    Dim v1 As Variant, v2 As Variant, i As Long, srcRng As Range, lRow As Long, fnd As Range
    Set desWB = ThisWorkbook
    Set desWS = ThisWorkbook.Sheets("Stock")
    desWS.UsedRange.Offset(1).ClearContents
    Set wb1 = Workbooks.Open("C:\Test\STOCK.xlsx")
    Set ws1 = Sheets("Stock")
    Set wb2 = Workbooks.Open("C:\Test\STO.xlsx")
    Set ws2 = Sheets("Stock")
    v1 = ws1.Range("B2", ws1.Range("B" & Rows.Count).End(xlUp)).Resize(, 3).Value
    v2 = ws2.Range("B2", ws2.Range("B" & Rows.Count).End(xlUp)).Resize(, 3).Value
    Set srcRng = ws2.Range("B2", ws2.Range("B" & Rows.Count).End(xlUp))
    For i = LBound(v1) To UBound(v1)
        If Not IsError(Application.Match(v1(i, 1), srcRng, 0)) Then
            x = Application.Match(v1(i, 1), srcRng, 0)
            With desWS
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                .Range("B" & lRow).Resize(, 3).Value = Array(v1(i, 1), v1(i, 2) + ws2.Range("C" & x + 1), v1(i, 3) + ws2.Range("D" & x + 1))
                .Range("E" & lRow) = .Range("C" & lRow) - .Range("D" & lRow)
            End With
        Else
            With desWS
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                .Range("B" & lRow).Resize(, 3).Value = Array(v1(i, 1), v1(i, 2), v1(i, 3))
                .Range("E" & lRow) = .Range("C" & lRow) - .Range("D" & lRow)
            End With
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        Set fnd = desWS.Range("B:B").Find(v2(i, 1), LookIn:=xlValues, lookat:=xlWhole)
        If fnd Is Nothing Then
            With desWS
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                .Range("B" & lRow).Resize(, 3).Value = Array(v2(i, 1), v2(i, 2), v2(i, 3))
                .Range("E" & lRow) = .Range("C" & lRow) - .Range("D" & lRow)
            End With
        End If
    Next i
    With desWS
        .Range("A2").Value = "1"
        .Range("A2").AutoFill Destination:=.Range("A2").Resize(.Range("B" & .Rows.Count).End(xlUp).Row - 1), Type:=xlFillSeries
    End With
    Application.DisplayAlerts = False
    desWB.SaveAs Filename:=desWB.Path & Application.PathSeparator & "Final Stock.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
great !

I have two things .

first of all I want close two files after pull the data from them

second can I make the code flexible if I have many files with the same structure in the same folder instead of specify two files as in code.
 
Upvote 0
Are you saying that you want to combine the data from more than 2 files? If so, upload one or two more files.
 
Upvote 0
is it important to upload another files?
the same thing . the same structure data without any different .
 
Upvote 0
Working with more than two files will be different from working with only two files. An extra file will allow me to test the changes that need to be made to the macro.
 
Upvote 0
An alternative is with Power Query. Bring both tables into the Power Query Editor and then the following Mcode

Power Query:
let
    Source = Table.Combine({Table1, Table2}),
    #"Grouped Rows" = Table.Group(Source, {"BRAND"}, {{"Total Import", each List.Sum([IMPORT]), type nullable number}, {"Total Export", each List.Sum([EXPORT]), type nullable number}}),
    #"Replaced Value" = Table.ReplaceValue(#"Grouped Rows",null,0,Replacer.ReplaceValue,{"Total Import","Total Export"}),
    #"Inserted Subtraction" = Table.AddColumn(#"Replaced Value", "Subtraction", each [Total Import] - [Total Export], type number),
    #"Renamed Columns" = Table.RenameColumns(#"Inserted Subtraction",{{"Subtraction", "Net"}})
in
    #"Renamed Columns"
 
Upvote 0
Change the folder path (in red) to suit your needs.
Rich (BB code):
Sub CombineData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, srcWB As Workbook, desWB As Workbook, desWS As Worksheet
    Dim v As Variant, i As Long, srcRng As Range, lRow As Long, fnd As Range
    Set desWB = ThisWorkbook
    Set desWS = ThisWorkbook.Sheets("Stock")
    desWS.UsedRange.Offset(1).ClearContents
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strextension = Dir(strPath & "*.xlsx")
    Do While strextension <> ""
        Set srcWB = Workbooks.Open(strPath & strextension)
        If srcWB.Name <> "Final Stock.xlsx" Then
            Set srcWS = Sheets("Stock")
            v = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp)).Resize(, 3).Value
            For i = LBound(v) To UBound(v)
                Set srcRng = desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp))
                If Not IsError(Application.Match(v(i, 1), srcRng, 0)) Then
                    x = Application.Match(v(i, 1), srcRng, 0)
                    With desWS
                        .Range("C" & x + 1) = .Range("C" & x + 1) + v(i, 2)
                        .Range("D" & x + 1) = .Range("D" & x + 1) + v(i, 3)
                        .Range("E" & x + 1) = .Range("C" & x + 1) - .Range("D" & x + 1)
                    End With
                Else
                    With desWS
                        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                        .Range("B" & lRow).Resize(, 3).Value = Array(v(i, 1), v(i, 2), v(i, 3))
                        .Range("E" & lRow) = .Range("C" & lRow) - .Range("D" & lRow)
                    End With
                End If
            Next i
        End If
        srcWB.Close False
        strextension = Dir
    Loop
    With desWS
        .Range("A2").Value = "1"
        .Range("A2").AutoFill Destination:=.Range("A2").Resize(.Range("B" & .Rows.Count).End(xlUp).Row - 1), Type:=xlFillSeries
    End With
    Application.DisplayAlerts = False
    desWB.SaveAs Filename:=desWB.Path & Application.PathSeparator & "Final Stock.xlsx", FileFormat:=51
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
great ! can you sort from small to big ELEC-1000 ,ELEC-1002,ELEC-1003 ....?
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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