match based on headers and cell and sheet name to summing amounts

tubrak

Board Regular
Joined
May 30, 2021
Messages
218
Office Version
  1. 2019
Platform
  1. Windows
Hello
I need populating sheets names in column A and I will write in cell C1 the month and headers B2:E2 across sheets
so if I write month in C1 JAN , then should populate list of sheets names in column A and match headers and should sum the amounts for each sheet under header is matched based on month or sum the amounts for each sheet under header is matched for whole months when C1 is empty and the last column BALANCE should calculate as I put it , but I don't need any formulas , the same thing should populate TOTAL values for each column and will change in location based on the last row contains sheet name.
every time I will add new sheets and with the same structure and will increase sheets in output sheet .
if can do by vba with deal big data for all of sheets will be a great.
here is data in multiple sheets


assss.xlsm
ABCDE
1DATEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023INV100221000010000
302/01/2023INV100231500025000
403/01/2023INV1002420000200043000
504/01/2023INV1002525000200066000
605/02/2023INV10026200068000
706/02/2023INV100271000078000
807/02/2023CASH100077000
908/02/2023BANK100076000
1009/02/2023CASH800068000
1110/02/2023CASH120066800
1211/02/2023INV10028234100066034
1312/02/2023INV100291230078334
AA
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3



assss.xlsm
ABCDE
1DATEINVOICE NUMBERDEBIT CUCREDIT CUBALANCE
201/01/2023INV10011000010000
302/01/2023INV10111500025000
403/01/2023CASH200023000
504/01/2023CASH200021000
605/02/2023INV1002200023000
706/02/2023INV10031000033000
807/02/2023BANK100032000
908/02/2023BANK100031000
1009/02/2023BANK800023000
1110/02/2023BANK120021800
1211/02/2023INV10081200100022000
1312/02/2023INV100922000
AS
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3




assss.xlsm
ABCDE
1DATEINVOICE NUMBERCREDIT CLDEBIT CLBALANCE
201/01/2023INV1002000020000
302/01/2023INV1012000040000
403/01/2023BANK200038000
504/01/2023BANK200036000
605/02/2023BANK100035000
706/02/2023BANK200033000
807/02/2023BANK1200021000
908/02/2023BANK120009000
1009/02/2023BANK10008000
1110/02/2023BANK2007800
1211/02/2023BANK1007700
1312/02/2023BANK1207580
ATM
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3


assss.xlsm
ABCDE
1DATEINVOICE NUMBERCREDITDEBIT CLBALANCE
201/01/2023INV1001000010000
302/01/2023INV1012000030000
403/01/2023CASH50000200078000
504/01/2023CASH200076000
605/02/2023CASH120074800
706/02/2023CASH12074680
807/02/2023CASH20074480
908/02/2023CASH1200062480
1009/02/2023CASH100061480
1110/02/2023CASH20061280
1211/02/2023CASH10061180
1312/02/2023CASH12061060
AB
Cell Formulas
RangeFormula
E2E2=C2
E3:E13E3=E2+C3-D3


before
assss.xlsm
ABCDEF
1
2sheets namesDEBIT CUCREDIT CUCREDIT CLDEBIT CLBALANCE
3
4
5
6
OUTPUT


after based on cell1 is month
assss.xlsm
ABCDEF
1JAN
2sheets namesDEBIT CUCREDIT CUCREDIT CLDEBIT CLBALANCE
3AA7000040004000
4AS2500040008000
5ATM4000040004000
6AB8000040000
7TOTAL95000800012000080000
OUTPUT
Cell Formulas
RangeFormula
F3,F7F3=C3-E3
F4:F6F4=F3+C4-E4
B7:E7B7=SUM(B3:B6)


after based on C1 is empty should sum whole of months
assss.xlsm
ABCDEF
1
2sheets namesDEBIT CUCREDIT CUCREDIT CLDEBIT CLBALANCE
3AA945341620016200
4AS382001620032400
5ATM4000032420-20
6AB8000018940-18960
7TOTAL1327343240012000051360-18960
OUTPUT
Cell Formulas
RangeFormula
F3,F7F3=C3-E3
F4:F6F4=F3+C4-E4
B7:E7B7=SUM(B3:B6)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi @tubrak , I hope you are well.

If you still need the macro, try the next one.

I assume that in cell C1 of the "Output" sheet you have the text "JAN" or the text of the first 3 letters of the month.
If you don't have a text and you have a date, then you should change this line in the macro:
VBA Code:
mnt = LCase(shO.Range("C1").Value)
By this line:
VBA Code:
mnt = LCase(Format(shO.Range("C1").Value, "mmm"))


VBA Code:
Sub match_based_on_headers()
  Dim dic As Object
  Dim sh As Worksheet, shO As Worksheet
  Dim a() As Variant, b As Variant
  Dim i As Long, k As Long, col As Long, lr As Long
  Dim c As Range
  Dim mnt As String
  Dim ant As Double
  
  Application.ScreenUpdating = False
  
  Set shO = Sheets("Output")
  Set dic = CreateObject("Scripting.Dictionary")
  shO.Range("A3:F" & Rows.Count).ClearContents
  shO.Range("A3:F" & Rows.Count).Interior.ColorIndex = xlNone
  mnt = LCase(shO.Range("C1").Value)
  
  If mnt = "" Then k = 2 Else k = 0
  ReDim b(1 To Sheets.Count, 1 To 6)
  
  For Each sh In Sheets
    If sh.Name <> shO.Name Then
      col = 0
      If Trim(sh.Range("C1").Value) = "DEBIT CU" Or Trim(sh.Range("D1").Value) = "CREDIT CU" Then col = 2
      If Trim(sh.Range("C1").Value) = "CREDIT CL" Or Trim(sh.Range("D1").Value) = "DEBIT CL" Then col = 4
      
      If col <> 0 Then
        If mnt = "" Then
          'sum whole months
          k = k + 1
          shO.Cells(k, "A").Value = sh.Name
          shO.Cells(k, col).Value = WorksheetFunction.Sum(sh.Range("C:C"))
          shO.Cells(k, col + 1).Value = WorksheetFunction.Sum(sh.Range("D:D"))
          shO.Cells(k, "F").Value = Val(shO.Cells(k - 1, "F").Value) + shO.Cells(k, "C").Value - shO.Cells(k, "E").Value
        Else
          'sum by month
          Erase a
          a = sh.Range("A1", sh.Range("D" & Rows.Count).End(3)).Value
          k = k + 1
          For i = 2 To UBound(a, 1)
            If Format(a(i, 1), "mmm") = mnt Then
              b(k, col) = b(k, col) + a(i, 3)
              b(k, col + 1) = b(k, col + 1) + a(i, 4)
            End If
          Next
          b(k, 1) = sh.Name
          If k = 1 Then ant = 0 Else ant = b(k - 1, 6)
          b(k, 6) = ant + b(k, 3) - b(k, 5)
        End If
      End If
    End If
  Next
  
  If mnt <> "" Then
    shO.Range("A3").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End If
  
  'populate TOTAL value
  lr = shO.Range("A" & Rows.Count).End(3).Row
  With shO.Range("B" & lr + 1 & ":E" & lr + 1)
    .Formula = "=SUM(B3:B" & lr & ")"
    .Value = .Value
  End With
  With shO.Range("A" & lr + 1)
    .Value = "TOTAL"
    .Interior.Color = vbYellow
    .Font.Bold = True
    .Offset(0, 5).Value = shO.Range("C" & lr + 1).Value - shO.Range("E" & lr + 1).Value
  End With
    
  Application.ScreenUpdating = False
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Upvote 1
Solution
Hi Dante,
after about 25 days I don't expect somebody help me after all of this period , indeed I lost hope to anybody answers me .
I see your macro works perfectly as I want it .(y)
many thanks for your help.:)
 
Upvote 0
Glad to know it works for you.
I also appreciate your effort to put a pretty clear example.
I hope it was worth the wait time.
:)
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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