Extraction same sheet name from files in folder and merge the same price

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
664
Office Version
  1. 2019
Hello,
I would macro, not POWER QUERY to extract the whole sheets from files are existed in directory D:\ab\INVOICES
so every file contains one sheet , the same sheet name .
INV1.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4.00500.002,000.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR4.00460.001,840.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00445.001,780.00
287GC 385/65R22.5 AT131 CHI2.001,650.003,300.00
298KM 265/70R16 KOR4.00880.003,520.00
309DONGA 66A L KOR2.00460.00920.00
3110KM 235/65R17 HP71 KOR4.00625.002,500.00
3211KM 235/55R19 PS71 KOR4.00675.002,700.00
3312DROUB 90A L KOR1.00575.00575.00
3413KM 13R22.5 MA11 KOR2.002,550.005,100.00
35TOTAL39.0028,485.00
SH1
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22



INV2.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1.00500.00500.00
232VEGA 55A R KOR2.00425.00850.00
243LARGEST 60A L HIGH JAP2.00440.00880.00
254KM 215/65R16 TA31 KOR1.00460.00460.00
265265/70R16 ALGERIA4.00630.002,520.00
276KM 205/65R16 HS63 KOR4.00450.001,800.00
287GC 385/65R22.5 AT131 CHI12.001,640.0019,680.00
298KM 265/70R16 KOR4.00880.003,520.00
309KM 235/65R17 HP71 KOR1.00875.00875.00
3110KM 235/55R19 PS71 KOR2.00670.001,340.00
32TOTAL33.0032,425.00
SH1
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22



INV3.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR10.002,555.0025,550.00
232KM 13R22.5 MA11 KOR12.002,000.0024,000.00
24TOTAL22.0049,550.00
SH1
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22



so the result in OUTPUT file into result sheet will already be existed and rename sheet for each file contains same sheet name based on file name INV1,INV2,INV3 before RESULT sheet. after data copy the data from sheets and merge QTY in column D for each brand IN COLUMN C if the price is the same thing in column E for duplicates brands whether in the same sheet or all of sheets across files otherwise don't merge , surely without forget insert TOTAL row to sum amounts.


before
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
22
23
24
25
26
27
28
RESULT




after
OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR5500.002,500.00
232VEGA 55A R KOR4425.001,700.00
243LARGEST 60A L HIGH JAP4440.001,760.00
254KM 215/65R16 TA31 KOR5460.002,300.00
265265/70R16 ALGERIA8630.005,040.00
276KM 205/65R16 HS63 KOR4445.001,780.00
287GC 385/65R22.5 AT131 CHI21,650.003,300.00
298KM 265/70R16 KOR8880.007,040.00
309DONGA 66A L KOR2460.00920.00
3110KM 235/65R17 HP71 KOR4625.002,500.00
3211KM 235/55R19 PS71 KOR4675.002,700.00
3312DROUB 90A L KOR1575.00575.00
3413KM 13R22.5 MA11 KOR22,550.005,100.00
3514KM 205/65R16 HS63 KOR4450.001,800.00
3615GC 385/65R22.5 AT131 CHI121,640.0019,680.00
3716KM 235/65R17 HP71 KOR1875.00875.00
3817KM 235/55R19 PS71 KOR2670.001,340.00
3918BJS 13R22.5 MA11 KOR102,555.0025,550.00
4019KM 13R22.5 MA11 KOR122,000.0024,000.00
41TOTAL94110,460.00
RESULT
Cell Formulas
RangeFormula
D41,F41D41=SUM(D22:D40)

every time when run macro should delete sheets before RESULT sheet also delete data in RESULT sheet before brings data.
thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Place this macro in a regular module in the "OUTPUT" workbook.
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim lRow As Long, dic As Object, v As Variant, i As Long, desWS As Worksheet, srcWB As Workbook, val As String, fnd As Range
    Set desWS = ThisWorkbook.Sheets("RESULT")
    Const strPath As String = "D:\ab\INVOICES\"
    ChDir strPath
    strExtension = Dir(strPath & "INV*.xlsm")
    Set dic = CreateObject("Scripting.Dictionary")
    Do While strExtension <> ""
        If strExtension Like "INV*" Then
            Set srcWB = Workbooks.Open(strPath & strExtension)
            With srcWB.Sheets("SH1")
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                v = .Range("C22:C" & lRow - 1).Resize(, 3).Value
                For i = LBound(v) To UBound(v)
                    val = v(i, 1) & "|" & v(i, 3)
                    If Not dic.exists(val) Then
                        dic.Add val, v(i, 2)
                        With desWS
                            .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(, 3).Value = Array(v(i, 1), v(i, 2), v(i, 3))
                        End With
                    Else
                        With desWS
                            Set fnd = .Range("C:C").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                            .Range("C" & fnd.Row).Resize(, 3).Value = Array(v(i, 1), v(i, 2) + dic(val), v(i, 3))
                        End With
                    End If
                Next i
                srcWB.Close savechanges:=False
            End With
        End If
        strExtension = Dir
    Loop
    With desWS
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("B22").Value = "1"
        .Range("B22").AutoFill Destination:=.Range("B22").Resize(lRow - 21), Type:=xlFillSeries
        .Range("B" & lRow + 1) = "TOTAL"
        .Range("F22:F" & lRow).Formula = "=D22*E22"
        .Range("D" & lRow + 1).Formula = "=sum(D22:D" & lRow & ")"
        .Range("F" & lRow + 1).Formula = "=sum(F22:F" & lRow & ")"
        .Range("E22:F" & lRow + 1).NumberFormat = "#,##0.00"
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thanks,
but shows error AutoFill method of the Range class failed in this line
VBA Code:
.Range("B22").AutoFill Destination:=.Range("B22").Resize(lRow - 21), Type:=xlFillSeries
 
Upvote 0
When I tested the macro using the data you posted, it worked with no error. Are you using the macro with the same data?
 
Upvote 0
Ok I see my bad !
extension xlsm excel in OP and I use with xls .

I forgot the sheets should add before result sheet for every file
the result in OUTPUT file should add sheets before combine data in RESULT sheet like this

OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR4500.002,000.00
232VEGA 55A R KOR2425.00850.00
243LARGEST 60A L HIGH JAP2440.00880.00
254KM 215/65R16 TA31 KOR4460.001,840.00
265265/70R16 ALGERIA4630.002,520.00
276KM 205/65R16 HS63 KOR4445.001,780.00
287GC 385/65R22.5 AT131 CHI21,650.003,300.00
298KM 265/70R16 KOR4880.003,520.00
309DONGA 66A L KOR2460.00920.00
3110KM 235/65R17 HP71 KOR4625.002,500.00
3211KM 235/55R19 PS71 KOR4675.002,700.00
3312DROUB 90A L KOR1575.00575.00
3413KM 13R22.5 MA11 KOR22,550.005,100.00
35TOTAL3928,485.00
INV1
Cell Formulas
RangeFormula
D35,F35D35=SUM(D22:D34)
F22:F34F22=E22*D22




OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR1500.00500.00
232VEGA 55A R KOR2425.00850.00
243LARGEST 60A L HIGH JAP2440.00880.00
254KM 215/65R16 TA31 KOR1460.00460.00
265265/70R16 ALGERIA4630.002,520.00
276KM 205/65R16 HS63 KOR4450.001,800.00
287GC 385/65R22.5 AT131 CHI121,640.0019,680.00
298KM 265/70R16 KOR4880.003,520.00
309KM 235/65R17 HP71 KOR1875.00875.00
3110KM 235/55R19 PS71 KOR2670.001,340.00
32TOTAL3332,425.00
INV2
Cell Formulas
RangeFormula
D32,F32D32=SUM(D22:D31)
F22:F31F22=E22*D22



OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221BJS 13R22.5 MA11 KOR102,555.0025,550.00
232KM 13R22.5 MA11 KOR122,000.0024,000.00
24TOTAL2249,550.00
INV3
Cell Formulas
RangeFormula
D24,F24D24=SUM(D22:D23)
F22:F23F22=D22*E22



OUTPUT.xlsm
BCDEF
21ITEMBRANDQTYPRICEBALANCE
221VEGA 70A R KOR5500.002,500.00
232VEGA 55A R KOR4425.001,700.00
243LARGEST 60A L HIGH JAP4440.001,760.00
254KM 215/65R16 TA31 KOR5460.002,300.00
265265/70R16 ALGERIA8630.005,040.00
276KM 205/65R16 HS63 KOR4445.001,780.00
287GC 385/65R22.5 AT131 CHI21,650.003,300.00
298KM 265/70R16 KOR8880.007,040.00
309DONGA 66A L KOR2460.00920.00
3110KM 235/65R17 HP71 KOR4625.002,500.00
3211KM 235/55R19 PS71 KOR4675.002,700.00
3312DROUB 90A L KOR1575.00575.00
3413KM 13R22.5 MA11 KOR22,550.005,100.00
3514KM 205/65R16 HS63 KOR4450.001,800.00
3615GC 385/65R22.5 AT131 CHI121,640.0019,680.00
3716KM 235/65R17 HP71 KOR1875.00875.00
3817KM 235/55R19 PS71 KOR2670.001,340.00
3918BJS 13R22.5 MA11 KOR102,555.0025,550.00
4019KM 13R22.5 MA11 KOR122,000.0024,000.00
41TOTAL94110,460.00
RESULT
Cell Formulas
RangeFormula
D41,F41D41=SUM(D22:D40)


sorry about missed details in OP
I hope you can modify the code.
 
Last edited:
Upvote 0
shouldn't repeat copying to the bottom that what I notice the code does it .
as I mentioned
every time when run macro should delete sheets before RESULT sheet also delete data in RESULT sheet before brings data.
 
Upvote 0
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim lRow As Long, dic As Object, v As Variant, i As Long
    Dim desWB As Workbook, desWS As Worksheet, srcWB As Workbook, val As String, fnd As Range
    Set desWB = ThisWorkbook
    Set desWS = desWB.Sheets("RESULT")
    With desWS.UsedRange
        .Offset(1).ClearContents
        .Borders.LineStyle = xlNone
    End With
    Const strPath As String = "D:\ab\INVOICES\"
    strExtension = Dir(strPath & "INV*.xlsm")
    Set dic = CreateObject("Scripting.Dictionary")
    Do While strExtension <> ""
        If strExtension Like "INV*" Then
            Set srcWB = Workbooks.Open(strPath & strExtension)
            Sheets("SH1").Copy before:=desWB.Sheets("RESULT")
            ActiveSheet.Name = Left(srcWB.Name, 4)
            srcWB.Close savechanges:=False
            With ActiveSheet
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                v = .Range("C22:C" & lRow - 1).Resize(, 3).Value
                For i = LBound(v) To UBound(v)
                    val = v(i, 1) & "|" & v(i, 3)
                    If Not dic.exists(val) Then
                        dic.Add val, v(i, 2)
                        With desWS
                            .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(, 3).Value = Array(v(i, 1), v(i, 2), v(i, 3))
                        End With
                    Else
                        With desWS
                            Set fnd = .Range("C:C").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                            .Range("C" & fnd.Row).Resize(, 3).Value = Array(v(i, 1), v(i, 2) + dic(val), v(i, 3))
                        End With
                    End If
                Next i
            End With
        End If
        strExtension = Dir
    Loop
    desWS.Activate
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("B22").Value = "1"
    Range("B22").AutoFill Destination:=Range("B22").Resize(lRow - 21), Type:=xlFillSeries
    Range("B" & lRow + 1) = "TOTAL"
    Range("F22:F" & lRow).Formula = "=D22*E22"
    Range("D" & lRow + 1).Formula = "=sum(D22:D" & lRow & ")"
    Range("F" & lRow + 1).Formula = "=sum(F22:F" & lRow & ")"
    Range("E22:F" & lRow + 1).NumberFormat = "#,##0.00"
    ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thanks , but when run the code every time should delete sheets before result sheet before add sheets again
the code will shows error about sheets names have already existed .
 
Upvote 0
Try:
VBA Code:
Sub CopySheetData()
    Application.ScreenUpdating = False
    Dim lRow As Long, dic As Object, v As Variant, i As Long, ws As Worksheet
    Dim desWB As Workbook, desWS As Worksheet, srcWB As Workbook, val As String, fnd As Range
    Set desWB = ThisWorkbook
    Set desWS = desWB.Sheets("RESULT")
    With desWS.UsedRange
        .Offset(1).ClearContents
        .Borders.LineStyle = xlNone
    End With
    With desWB
        Application.DisplayAlerts = False
        For Each ws In .Sheets
            If ws.Name <> desWS.Name Then
                ws.Delete
            End If
        Next ws
        Application.DisplayAlerts = True
    End With
    Const strPath As String = "D:\ab\INVOICES\"
    strExtension = Dir(strPath & "INV*.xlsm")
    Set dic = CreateObject("Scripting.Dictionary")
    Do While strExtension <> ""
        If strExtension Like "INV*" Then
            Set srcWB = Workbooks.Open(strPath & strExtension)
            Sheets("SH1").Copy before:=desWB.Sheets("RESULT")
            ActiveSheet.Name = Split(srcWB.Name, ".")(0)
            srcWB.Close savechanges:=False
            With ActiveSheet
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                v = .Range("C22:C" & lRow - 1).Resize(, 3).Value
                For i = LBound(v) To UBound(v)
                    val = v(i, 1) & "|" & v(i, 3)
                    If Not dic.exists(val) Then
                        dic.Add val, v(i, 2)
                        With desWS
                            .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Resize(, 3).Value = Array(v(i, 1), v(i, 2), v(i, 3))
                        End With
                    Else
                        With desWS
                            Set fnd = .Range("C:C").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                            .Range("C" & fnd.Row).Resize(, 3).Value = Array(v(i, 1), v(i, 2) + dic(val), v(i, 3))
                        End With
                    End If
                Next i
            End With
        End If
        strExtension = Dir
    Loop
    desWS.Activate
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("B22").Value = "1"
    Range("B22").AutoFill Destination:=Range("B22").Resize(lRow - 21), Type:=xlFillSeries
    Range("B" & lRow + 1) = "TOTAL"
    Range("F22:F" & lRow).Formula = "=D22*E22"
    Range("D" & lRow + 1).Formula = "=sum(D22:D" & lRow & ")"
    Range("F" & lRow + 1).Formula = "=sum(F22:F" & lRow & ")"
    Range("E22:F" & lRow + 1).NumberFormat = "#,##0.00"
    ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,504
Messages
6,185,360
Members
453,288
Latest member
rlmorales2000

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