Populate summary by sorting data for customer for each sheet alone based on dates

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I would create report for each sheet alone for each customer .
I need macro to sort big data for each sheet (about 11000 rows for each sheet alone)
every customer in column C should merge amount in column J with exclude TOTAL row until doesn't merging twice for each sheet alone, just notice VOUCHER sheet doesn't contain TOTAL row. so when merge amount ,then should search for TOTAL column with exclude TOTAL row if it's existed
merging for each sheet will be based on two condition
first if I write two dates in D5,F5

some data for some sheets
KM.xlsm
ABCDEFGHIJ
1ITEMDATENAMEINVOICEGOODSTYPEPRQTYUNITTOTAL
2119/07/2023CR-1000STVG-1000ATRAM1MTR525125
3219/07/2023CR-1000STVG-1000ATRAM2PO435140
4319/07/2023CR-1000STVG-1000ATRAM1SO24590
5TOTAL355
6120/07/2023CR-1001STVG-1001ATRAM1GR22346
7220/07/2023CR-1001STVG-1001ATRAM2PO23570
8TOTAL116
9121/07/2023CR-1000STVG-1000ATRAM1MTR22550
10221/07/2023CR-1000STVG-1000ATRAM2PO23570
11321/07/2023CR-1000STVG-1000ATRAM1SO24590
12TOTAL210
BUYING




KM.xlsm
ABCDEFGHIJ
1ITEMDATENAMEINVOICEGOODSTYPEPRQTYUNITTOTAL
2119/07/2023CR-1000FRVG-1000ATRAM1GR55221210
3219/07/2023CR-1000FRVG-1000ATRAM2PO1433462
4319/07/2023CR-1000FRVG-1000ATRAM1SO1044440
5TOTAL2112
6120/07/2023CR-1001FRVG-1001ATRAM1GR1022220
7220/07/2023CR-1001FRVG-1001ATRAM2PO1033330
8TOTAL550
9120/07/2023CR-1001FRVG-1002ATRAM1GR522110
10220/07/2023CR-1001FRVG-1002ATRAM2PO1030300
11TOTAL410
12121/07/2023CR-1002FRVG-1003ATRAM3GR1025250
13221/07/2023CR-1002FRVG-1003ATRAM4PO1025250
14TOTAL500
SALES



KM.xlsm
ABCDEFG
1ITEMDATENAMEVC. NODEBITCREDITTOTAL
2119/07/2023CR-1000CR-10012,000.002,000.00
3220/07/2023CR-1000CR-10012,000.002,000.00
4321/07/2023CR-1000CR-10012,500.00-2,500.00
5422/07/2023CR-1001CR-10011,000.00-1,000.00
VOUCHER











original


KM.xlsm
ABCDEF
4FROM DATEFROM DATE
5
6
7
8ITEMNAMEBUYINGSALESVOUCHERNET
REPORT




expected
KM.xlsm
ABCDEF
4FROM DATEFROM DATE
519/07/202320/07/2023
6
7
8ITEMNAMEBUYINGSALESVOUCHERNET
91CR-1000355.002,112.004,000.00-1,888.00
102CR-1001116.00960.00960.00
REPORT
Cell Formulas
RangeFormula
F9:F10F9=D9-E9



and if D5,F5 are empty then should populate like this


KM.xlsm
ABCDEF
4FROM DATEFROM DATE
5
6
7
8ITEMNAMEBUYINGSALESVOUCHERNET
91CR-1000565.002,112.001,500.00612.00
102CR-1001116.00960.00-1,000.001,960.00
113CR-1002500.00
REPORT
Cell Formulas
RangeFormula
F9:F10F9=D9-E9


every sheet will contains duplicate customer should merge , some customers are not existed in all of sheets .
last thing I will add new sheets before REPORT sheet with the same structure .
I hope my data are clear.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Review the following considerations:

1. TOTAL column.
then should search for TOTAL column
The macro considers the last column of data to be the TOTAL column in all sheets. It's right?


2.
How is the NET column calculated?
last thing I will add new sheets before REPORT sheet with the same structure .
If you are going to add new sheets, how do you consider the formula to calculate the NET column?


3. The sheets to be considered should be in row 8 of the REPORT sheet.

1726272475074.png


If you agree with the above considerations. Try the following macro:


VBA Code:
Sub Populate_Summary()
  Dim a As Variant, b As Variant, ky As Variant
  Dim shR As Worksheet, sh As Worksheet
  Dim dic1 As Object, dic2 As Object
  Dim s&, lc&, i&, j&, k&, y&
  Dim nRow&, nCol&, rowTot&
  Dim nSheet As String
  Dim froDate As Long, to_Date As Long
  Dim iniDate As Long, finDate As Long
  
  
  Set shR = Sheets("Report")
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  
  'Read sheet names from the Report sheet
  lc = shR.Cells(8, Columns.Count).End(1).Column
  For j = 3 To lc - 1
    nSheet = shR.Cells(8, j).Value
    If Evaluate("ISREF('" & nSheet & "'!A1)") Then
      Set sh = Sheets(nSheet)
      rowTot = rowTot + sh.Range("A" & Rows.Count).End(3).Row
      dic2(nSheet) = j
    End If
  Next
  ReDim b(1 To rowTot, 1 To lc)
  froDate = shR.Range("D5").Value
  to_Date = shR.Range("F5").Value
  
  'Read each sheet
  For Each ky In dic2.keys
    Set sh = Sheets(ky)
    lc = sh.Cells(1, Columns.Count).End(1).Column
    a = sh.Range("A1", sh.Cells(sh.Range("A" & Rows.Count).End(3).Row, lc)).Value2
    For i = 2 To UBound(a)
      If UCase(a(i, 1)) <> "TOTAL" Then
      
        If froDate = 0 Then iniDate = a(i, 2) Else iniDate = froDate
        If to_Date = 0 Then finDate = a(i, 2) Else finDate = to_Date
      
        If a(i, 2) >= iniDate And a(i, 2) <= finDate Then
          If Not dic1.exists(a(i, 3)) Then
            y = y + 1
            dic1(a(i, 3)) = y
          End If
          nRow = dic1(a(i, 3))
          nCol = dic2(sh.Name)
          b(nRow, 1) = nRow
          b(nRow, 2) = a(i, 3)
          b(nRow, nCol) = b(nRow, nCol) + a(i, lc)
        End If
      End If
    Next
  Next
  
  shR.Range("A9").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub


😇
 
Upvote 0
Solution
The macro considers the last column of data to be the TOTAL column in all sheets. It's right?
Yes.
2. How is the NET column calculated?
as I posted !
If you are going to add new sheets, how do you consider the formula to calculate the NET column?
will not change when add new sheets at all.
thank you
 
Upvote 0
2. How is the NET column calculated?
as I posted !
After this line:
VBA Code:
b(nRow, nCol) = b(nRow, nCol) + a(i, lc)

Add this line:
VBA Code:
b(nRow, UBound(b, 2)) = b(nRow, 4) - b(nRow, 5)

🤗
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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