copy multiple columns and matched from multiple sheets to sheet summary and calculate

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
hi experts,
I know what I'm asking is not easy but i hope to see my post by any has knowing in vba so I have multiple sheets are the same structure should match COL B,C,D with all sheets in all sheets then should create the all of data in sheet summary and calculate the values
I put the formula how should calculate the values and sorry if my data are poor , just tell me if that's not clear
sheet "stock"
REPORT1.xlsx
ABCDE
1itemBRANDTYPEMONAFACTUREQTY
2110W40 208LQ8EU1000
3215W40 208LCASSU400
435W30 208LQ8EU800
545W30 12x1LQ8EU600
6510W40 208LENIIT300
765W30 4x4LQ8EU200
8710W40 12x1LQ8EU120
9815W40 12x1LCASSU450
10910W40 12x1LENIIT890
111010W40 4x4LQ8EU345
121110W40 4x4LCASSU78
131210W40 4x4LENIIT123
14135W40 4x4LQ8EU456
15145W40 4x4LCASSU678
16155W40 4x4LENIIT1234
171620W50 4x4LQ8EU456
stock


sheet sales
REPORT1.xlsx
ABCDE
1itemBRANDTYPEMONAFACTURESALES
2110W40 208LQ8EU100
3215W40 208LCASSU50
435W30 208LQ8EU280
545W30 12x1LQ8EU300
6510W40 208LENIIT80
765W30 4x4LQ8EU20
8710W40 12x1LQ8EU20
9815W40 12x1LCASSU20
10910W40 12x1LENIIT876
111010W40 4x4LQ8EU345
121110W40 4x4LCASSU123
131210W40 4x4LENIIT78
14135W40 4x4LQ8EU300
15145W40 4x4LCASSU34
16155W40 4x4LENIIT23
171620W50 4x4LQ8EU56
sales


sheets "pur "
REPORT1.xlsx
ABCDE
1itemBRANDTYPEMONAFACTUREPURCHASE
2110W40 208LQ8EU55
3215W40 208LCASSU20
435W30 208LQ8EU10
545W30 12x1LQ8EU10
6510W40 208LENIIT3
765W30 4x4LQ8EU4
8710W40 12x1LQ8EU45
9815W40 12x1LCASSU8
10910W40 12x1LENIIT1
111010W40 4x4LQ8EU100
121110W40 4x4LCASSU20
131210W40 4x4LENIIT100
14135W40 4x4LQ8EU44
15145W40 4x4LCASSU20
16155W40 4x4LENIIT50
171620W50 4x4LQ8EU12
181720W50 4x4LCASSU9
191820W50 4x4LENIIT4
pur



sheet returns
REPORT1.xlsx
ABCDE
1itemBRANDTYPEMONAFACTUREreturns
2110W40 12x1LENIIT20
3210W40 4x4LQ8EU30
4310W40 4x4LCASSU40
545W30 12x1LQ8EU10
returns


expected result in sheet "summary"
REPORT1.xlsx
ABCDEFGHI
1itemBRANDTYPEMONAFACTURESTOCKSALESPURRETURNSBALANCE
2110W40 208LQ8EU100010055955
3215W40 208LCASSU4005020370
435W30 208LQ8EU80028010530
545W30 12x1LQ8EU6003001010320
6510W40 208LENIIT300803223
765W30 4x4LQ8EU200204184
8710W40 12x1LQ8EU1202045145
9815W40 12x1LCASSU450208438
10910W40 12x1LENIIT89087612035
111010W40 4x4LQ8EU34534510030130
121110W40 4x4LCASSU78123204015
131210W40 4x4LENIIT12378100145
14135W40 4x4LQ8EU45630044200
15145W40 4x4LCASSU6783420664
16155W40 4x4LENIIT123423501261
171620W50 4x4LQ8EU4565612412
181720W50 4x4LCASSU99
191820W50 4x4LENIIT44
20
summary
Cell Formulas
RangeFormula
I2:I19I2=E2-F2+G2+H2
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,​
why in the returns worksheet the items have not the same item # according to previous worksheets ?​
 
Last edited by a moderator:
Upvote 0
Assuming all 5 sheets already exists but there is nothing in 'summary' sheet, try this with a copy of your workbook.
If there is already data in 'summary' what should happen when this code is run?
- new data appended to bottom of existing data, or
- existing data cleared before new data is entered, or
- something else

VBA Code:
Sub CollateData()
  Dim d As Object
  Dim ShList As Variant, a As Variant, vals As Variant
  Dim i As Long, j As Long
  Dim s As String
  
  Set d = CreateObject("Scripting.Dictionary")
  ShList = Split("stock|sales|pur|returns", "|")
  For j = 0 To UBound(ShList)
    With Sheets(ShList(j))
      a = .UsedRange.Value2
      For i = 2 To UBound(a)
        s = Join(Application.Index(a, i, Array(2, 3, 4)), ";")
        If Not d.exists(s) Then d(s) = ";;;"
        vals = Split(d(s), ";")
        vals(j) = a(i, 5)
        d(s) = Join(vals, ";")
      Next i
    End With
  Next j
  Application.ScreenUpdating = False
  With Sheets("summary")
    With .Range("B2:C2").Resize(d.Count)
      .Value = Application.Transpose(Array(d.Keys, d.Items))
      With .Columns(2)
        .TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        .Offset(, 4).FormulaR1C1 = "=RC[-4]-RC[-3]+RC[-2]+RC[-1]"
        .Resize(, 2).EntireColumn.Insert
      End With
      .Columns(1).TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
      With .Columns(0)
        .Cells(1).Value = 1
        .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
      End With
    End With
    With .Range("A1:I1")
      .Value = Array("item", "BRAND", "TYPE", "MONAFACTURE", "STOCK", "SALES", "PUR", "RETURNS", "BALANCE")
      .EntireColumn.AutoFit
    End With
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
@Peter_SSs great code! thanks
but show empty data in row 18 and it doesn't show the borders in last row as the rest rows I no know why and I would hide the formula in last column in BALANCE just show as value if that's possible
about this
- existing data cleared before new data is entered
yes should be
REPORT1.xlsx
ABCDEFGHIJK
1itemBRANDTYPEMONAFACTURESTOCKSALESPURRETURNSBALANCE
2110W40 208LQ8EU100010055955
3215W40 208LCASSU4005020370
435W30 208LQ8EU80028010530
545W30 12x1LQ8EU6003001010320
6510W40 208LENIIT300803223
765W30 4x4LQ8EU200204184
8710W40 12x1LQ8EU1202045145
9815W40 12x1LCASSU450208438
10910W40 12x1LENIIT89087612035
111010W40 4x4LQ8EU34534510030130
121110W40 4x4LCASSU78123204015
131210W40 4x4LENIIT12378100145
14135W40 4x4LQ8EU45630044200
15145W40 4x4LCASSU6783420664
16155W40 4x4LENIIT123423501261
171620W50 4x4LQ8EU4565612412
18170
191820W50 4x4LCASSU99
201920W50 4x4LENIIT44
summary
Cell Formulas
RangeFormula
I2:I20I2=E2-F2+G2+H2
 
Upvote 0
show empty data in row 18
One or more of your input sheets must have one or more blank rows within the data. This possibility wasn't shown in your sample data. ;)

doesn't show the borders in last row
Sorry, didn't realise that borders were part of what you were asking

hide the formula in last column in BALANCE just show as value
No problem

yes should be
No problem.

See if this takes care of the above issues.

VBA Code:
Sub CollateData_v2()
  Dim d As Object
  Dim ShList As Variant, a As Variant, vals As Variant
  Dim i As Long, j As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  ShList = Split("stock|sales|pur|returns", "|")
  For j = 0 To UBound(ShList)
    With Sheets(ShList(j))
      a = .UsedRange.Value2
      For i = 2 To UBound(a)
        s = Join(Application.Index(a, i, Array(2, 3, 4)), ";")
        If Len(s) > 2 Then
          If Not d.exists(s) Then d(s) = ";;;"
          vals = Split(d(s), ";")
          vals(j) = a(i, 5)
          d(s) = Join(vals, ";")
        End If
      Next i
    End With
  Next j
  Application.ScreenUpdating = False
  With Sheets("summary")
    .UsedRange.EntireRow.Delete
    With .Range("B2:C2").Resize(d.Count)
      .Value = Application.Transpose(Array(d.Keys, d.Items))
      With .Columns(2)
        .TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        With .Offset(, 4)
          .FormulaR1C1 = "=RC[-4]-RC[-3]+RC[-2]+RC[-1]"
          .Value = .Value
        End With
        .Resize(, 2).EntireColumn.Insert
      End With
      .Columns(1).TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
      With .Columns(0)
        .Cells(1).Value = 1
        .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
      End With
    End With
    With .Range("A1:I1")
      .Value = Array("item", "BRAND", "TYPE", "MONAFACTURE", "STOCK", "SALES", "PUR", "RETURNS", "BALANCE")
      .EntireColumn.AutoFit
    End With
    With .UsedRange
      .BorderAround LineStyle:=xlContinuous
      .Borders(xlInsideVertical).LineStyle = xlContinuous
      .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
very excellent !
ok just I want bold the word of the headers and highlight the interior color by grey
thanks again
 
Upvote 0
bold the word of the headers and highlight the interior color by grey
Add these two blue lines where shown
Rich (BB code):
With .Range("A1:I1")
  .Value = Array("item", "BRAND", "TYPE", "MONAFACTURE", "STOCK", "SALES", "PUR", "RETURNS", "BALANCE")
  .Font.Bold = True
  .Interior.Color = RGB(166, 166, 166)
  .EntireColumn.AutoFit
End With
 
Upvote 0

Forum statistics

Threads
1,215,203
Messages
6,123,627
Members
449,109
Latest member
Sebas8956

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