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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Will each "stock" sheet in the two workbooks have the same number of items?
 
Upvote 0
not always as you see sometimes there is item in file but not existed in another for instance in row 20,21 are existed in first file but not in the second also the row4 is existed in file2 but not in file1 .I hope understanding you and this what you mean.
 
Upvote 0
Start by opening a new, blank workbook. Change the name of the first sheet to "Stock". Insert the following headers in row 1.
ITEMBRANDIMPORTEXPORTBALANCE

Copy/paste the macro below in a regular module. Change the folder path of the the two files (in red) and the file names (in blue) to suit your needs. Save the workbook as a macro-enabled file in a folder of your choice. Run the macro.
Rich (BB 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")
    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"
        .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
thanks . it gives error application defined error in this line
VBA Code:
.AutoFill Destination:=Range("A2").Resize(Range("B" & Rows.Count).End(xlUp).Row - 1), Type:=xlFillSeries

and when run the macro every time should not repeat copying data have already existed to the bottom just replace data ( should delete data and copy from the beginning).
 
Upvote 0
Insert this line of code:
VBA Code:
desWS.UsedRange.Offset(1).ClearContents
directly below this line:
VBA Code:
Set desWS = ThisWorkbook.Sheets("Stock")
I tested the macro using the data you posted and it worked properly without any error. Use the XL2BB add-in to post a copy of the sheet showing the result after you run the macro. Also, make sure that any cells that do not contain any values in columns C and D in the two "Stock" sheets, are blank.
 
Upvote 0
make sure that any cells that do not contain any values in columns C and D in the two "Stock" sheets, are blank.
yes . I can't contral the empty cell ( not always contains values somtimes should be blank)
 
Upvote 0
If some cells in the Import and Export columns have no value then those cells must be blank or contain a zero. If they contain anything else, an error will be generated.
 
Upvote 0
do you mean there is no way to avoid blank cells?
what if create "-" for any cells by code to avoid this problem. is it possible?
look the problem seems just from the serial numbers in column A . this is what I got after running
FINAL STOCK.xlsm
ABCDE
1ITEMBRANDIMPORTEXPORTBALANCE
21ELEC-1000210.00-210.00
3ELEC-10011,210.00-1,210.00
4ELEC-1002-144.00-144.00
5ELEC-10031,420.00-1,420.00
6ELEC-1004120.004.00116.00
7ELEC-1005400.00-400.00
8ELEC-1006240.00-240.00
9ELEC-1007133.00-133.00
10ELEC-1008133.00-133.00
11ELEC-1009134.00-134.00
12ELEC-10101,132.00-1,132.00
13ELEC-1011134.0010.00124.00
14ELEC-1012123.004.00119.00
15ELEC-1013133.0010.00123.00
16ELEC-1014134.003.00131.00
17ELEC-1015123.00-123.00
18ELEC-1016131.00-131.00
19ELEC-101722.002.0020.00
20ELEC-101812.00-12.00
21ELEC-101910.0012.00-2.00
22ELEC-102220.0012.008.00
stock
 
Upvote 0
I can't reproduce the problem. Could you upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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