Consolidate data from multiple sheets

ExcelNewbie2020

Active Member
Joined
Dec 3, 2020
Messages
343
Office Version
  1. 365
Platform
  1. Windows
I have multiple sheets (accumulated daily) with similar formats and the date is found cell A1. I hope its possible to extract and consolidate some data of those sheet and put it in a separate sheet. As shown below table.


Book1
ABCDEFGHIJKLMNOPQRSTUVW
1
2Sheet1Sheet2Sheet3Sheet4
320-01-202321-01-202322-01-202323-01-2023
4
5
6
7DeptNameAmountDeptNameAmountDeptNameAmountDeptNameAmount
8Acctgname11Marketingname45Salesname79Acctgname1013
9Marketingname22Salesname56Acctgname810Marketingname1114
10Salesname33Acctgname67Acctgname911HRname1215
11HRname44HRname78HRname1012HRname1316
12
13
14
15
16
17
18
19
20
21
22SHEET 5
23EXPECTED RESULT
24
25
26ACCTGMarketingSalesHR
27DateNameAmountDateNameAmountDateNameAmountDateNameAmount
2820-01-2023name1120-01-2023name2220-01-2023name3320-01-2023name44
2921-01-2023name6721-01-2023name4521-01-2023name5621-01-2023name78
3022-01-2023name81023-01-2023name111422-01-2023name7922-01-2023name1012
3122-01-2023name91123-01-2023name1215
3223-01-2023name101323-01-2023name1316
33
34
35
Sheet1
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Here VBA solution

VBA Code:
Sub jec()
 Dim dic As Object, sh, ar, ky, x As Long, i As Long
 Set dic = CreateObject("scripting.dictionary")
 
 For Each sh In Sheets(Array(1, 2, 3, 4))
    ar = sh.Range("A5").CurrentRegion
    For i = 2 To UBound(ar)
        If Not dic.exists(ar(i, 1)) Then dic(ar(i, 1)) = Array(ar(i, 1), CreateObject("scripting.dictionary"))
        dic(ar(i, 1))(1)(dic(ar(i, 1))(1).Count) = Array(CLng(sh.Cells(1, 1)), ar(i, 2), ar(i, 3))
    Next
 Next
 With Sheets(5).Cells(1, 1)
   For Each ky In dic.keys
      .Offset(, x) = ky
      .Offset(1, x).Resize(dic(ky)(1).Count, 3) = Application.Index(dic(ky)(1).items, 0, 0)
      x = x + 6
   Next
 End With
End Sub


I got started with this format (in the first 4 sheets)
- date in A1
- data starting from A5

Book1
ABC
120-1-2023
2
3
4
5DeptNameAmount
6Acctgname11
7Marketingname22
8Salesname33
9HRname44
Sheet1


This is the output

Book1
ABCDEFGHIJKLMNOPQRSTU
1AcctgMarketingSalesHR
220-1-2023name1120-1-2023name2220-1-2023name3320-1-2023name44
321-1-2023name6721-1-2023name4521-1-2023name5621-1-2023name78
422-1-2023name81023-1-2023name111422-1-2023name7922-1-2023name1012
522-1-2023name91123-1-2023name1215
623-1-2023name101323-1-2023name1316
7
8
9
Sheet5
 
Upvote 0
Forgot the headers

VBA Code:
Sub jec()
 Dim dic As Object, sh, ar, ky, x As Long, i As Long
 Set dic = CreateObject("scripting.dictionary")
 
 For Each sh In Sheets(Array(1, 2, 3, 4))
    ar = sh.Range("A5").CurrentRegion
    For i = 2 To UBound(ar)
        If Not dic.exists(ar(i, 1)) Then dic(ar(i, 1)) = Array(ar(i, 1), CreateObject("scripting.dictionary"))
        dic(ar(i, 1))(1)(dic(ar(i, 1))(1).Count) = Array(CLng(sh.Cells(1, 1)), ar(i, 2), ar(i, 3))
    Next
 Next
 With Sheets(5).Cells(1, 1)
   For Each ky In dic.keys
      .Offset(, x) = ky
      .Offset(1, x) = Array("Date", "Name", "Amount")
      .Offset(2, x).Resize(dic(ky)(1).Count, 3) = Application.Index(dic(ky)(1).items, 0, 0)
      x = x + 6
   Next
 End With
End Sub
 
Upvote 0
Previous code could be deleted.
This is the one, I missed a word

VBA Code:
Sub jec()
 Dim dic As Object, sh, ar, ky, x As Long, i As Long
 Set dic = CreateObject("scripting.dictionary")
 
 For Each sh In Sheets(Array(1, 2, 3, 4))
    ar = sh.Range("A5").CurrentRegion
    For i = 2 To UBound(ar)
        If Not dic.exists(ar(i, 1)) Then dic(ar(i, 1)) = Array(ar(i, 1), CreateObject("scripting.dictionary"))
        dic(ar(i, 1))(1)(dic(ar(i, 1))(1).Count) = Array(CLng(sh.Cells(1, 1)), ar(i, 2), ar(i, 3))
    Next
 Next
 With Sheets(5).Cells(1, 1)
   For Each ky In dic.keys
      .Offset(, x) = ky
      .Offset(1, x).Resize(,3) = Array("Date", "Name", "Amount")
      .Offset(2, x).Resize(dic(ky)(1).Count, 3) = Application.Index(dic(ky)(1).items, 0, 0)
      x = x + 6
   Next
 End With
End Sub
 
Upvote 0
Previous code could be deleted.
This is the one, I missed a word

VBA Code:
Sub jec()
 Dim dic As Object, sh, ar, ky, x As Long, i As Long
 Set dic = CreateObject("scripting.dictionary")
 
 For Each sh In Sheets(Array(1, 2, 3, 4))
    ar = sh.Range("A5").CurrentRegion
    For i = 2 To UBound(ar)
        If Not dic.exists(ar(i, 1)) Then dic(ar(i, 1)) = Array(ar(i, 1), CreateObject("scripting.dictionary"))
        dic(ar(i, 1))(1)(dic(ar(i, 1))(1).Count) = Array(CLng(sh.Cells(1, 1)), ar(i, 2), ar(i, 3))
    Next
 Next
 With Sheets(5).Cells(1, 1)
   For Each ky In dic.keys
      .Offset(, x) = ky
      .Offset(1, x).Resize(,3) = Array("Date", "Name", "Amount")
      .Offset(2, x).Resize(dic(ky)(1).Count, 3) = Application.Index(dic(ky)(1).items, 0, 0)
      x = x + 6
   Next
 End With
End Sub
thanks man it works.. I wonder, how can i tweak this code if there are more than 20 sheets i need to consolidate?.. really appreciated
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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