Extract duplicates dates and insert TOTAL row for each duplicates dates

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
Hello
I need macro for 8000 rows to extract duplicates dates in columns A B,C,F and ignore columns D,E in result sheet to DATE sheet and .
and insert TOTAL row for each duplicates dates .
DATE.xlsm
ABCDEF
1DATES.NACRRUING/CASHDEBITCREDITBALANCE
201/01/20241CASH IN CS2002,000.002,000.00
301/01/20242BANK IN BN20002,000.004,000.00
401/01/20243CASH IN CS20013,100.007,100.00
501/01/20244CASH IN CS204730.006,370.00
601/01/20245CASH IN CS2051,040.005,330.00
701/01/20246BANK IN BN2001620.004,710.00
801/01/20247CASH IN CS203200.004,910.00
901/01/20248BANK IN BN2002400.005,310.00
1002/01/20249CASH IN CS20031,030.006,340.00
1102/01/202410CASH IN CS206480.005,860.00
1203/01/202411CASH IN CS2071,040.004,820.00
1303/01/202412BANK IN BN2004620.004,200.00
14TOTAL8,730.004,530.004,200.00
RESULT
Cell Formulas
RangeFormula
F2,F14F2=D2-E2
F3:F13F3=F2+D3-E3
D14:E14D14=SUM(D2:D13)



DATE.xlsm
ABCDE
1ITEMDATES.NACRRUING/CASHBALANCE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DATE



what I want like this
DATE.xlsm
ABCDE
1ITEMDATES.NACRRUING/CASHBALANCE
2101/01/20241CASH IN CS2002,000.00
3201/01/20242BANK IN BN20004,000.00
4301/01/20243CASH IN CS20017,100.00
5401/01/20244CASH IN CS2046,370.00
6501/01/20245CASH IN CS2055,330.00
7601/01/20246BANK IN BN20014,710.00
8701/01/20247CASH IN CS2034,910.00
9801/01/20248BANK IN BN20025,310.00
10TOTAL39,730.00
11102/01/20249CASH IN CS20036,340.00
12202/01/202410CASH IN CS2065,860.00
13TOTAL12,200.00
14103/01/202411CASH IN CS2074,820.00
15203/01/202412BANK IN BN20044,200.00
16TOTAL9,020.00
DATE
Cell Formulas
RangeFormula
E10E10=SUM(E2:E9)
E13,E16E13=SUM(E11:E12)

in DATE sheet should clear data before brings data.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Please try the following on a copy of your workbook. Assumes the structure is exactly as you describe above before running the code.

VBA Code:
Option Explicit
Sub Alaamg()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("RESULT")
    Set ws2 = Worksheets("DATE")
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
    
    Set rngList = ws1.Range("A1").CurrentRegion
    ws1.Range("J1").Resize(2, 1).Value2 = Application.Transpose(Array("S.N", "<>"))
    Set rngCriteria = ws1.Range("J1").CurrentRegion
    ws2.Range("A1").CurrentRegion.Offset(1).ClearContents
    Set rngCopyTo = ws2.Range("B1:E1")
    rngList.AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    rngCriteria.ClearContents
    
    With ws2.Range("A2:A" & ws2.Cells(Rows.Count, "C").End(xlUp).Row)
        .FormulaR1C1 = "=IF(R[-1]C2<>RC2,1,1+R[-1]C)"
        .Value = .Value
    End With
    
    Dim i As Long
    For i = ws2.Cells(Rows.Count, "C").End(xlUp).Row To 3 Step -1
        If ws2.Cells(i, 1) = 1 Then ws2.Rows(i).EntireRow.Insert
    Next i
    
    Dim r As Range
    For Each r In ws2.Range("E:E").SpecialCells(xlCellTypeConstants).Areas
        r.Cells(r.Count + 1).Value = WorksheetFunction.Sum(r)
        r.Cells(r.Count + 1).Offset(, -3).Value = "TOTAL"
    Next r
    Application.ScreenUpdating = False
End Sub

RESULT sheet used:
Book1
ABCDEF
1DATES.NACRRUING/CASHDEBITCREDITBALANCE
21/01/20241CASH IN CS2002,000.002,000.00
31/01/20242BANK IN BN20002,000.004,000.00
41/01/20243CASH IN CS20013,100.007,100.00
51/01/20244CASH IN CS204730.006,370.00
61/01/20245CASH IN CS2051,040.005,330.00
71/01/20246BANK IN BN2001620.004,710.00
81/01/20247CASH IN CS203200.004,910.00
91/01/20248BANK IN BN2002400.005,310.00
102/01/20249CASH IN CS20031,030.006,340.00
112/01/202410CASH IN CS206480.005,860.00
123/01/202411CASH IN CS2071,040.004,820.00
133/01/202412BANK IN BN2004620.004,200.00
14TOTAL8,730.004,530.004,200.00
15
RESULT
Cell Formulas
RangeFormula
F2,F14F2=D2-E2
F3:F13F3=F2+D3-E3
D14:E14D14=SUM(D2:D13)


DATE sheet before running code:
Book1
ABCDE
1ITEMDATES.NACRRUING/CASHBALANCE
2
3
4
5
6
7
DATE


DATE sheet after running code:
Book1
ABCDE
1ITEMDATES.NACRRUING/CASHBALANCE
211/01/20241CASH IN CS2002,000.00
321/01/20242BANK IN BN20004,000.00
431/01/20243CASH IN CS20017,100.00
541/01/20244CASH IN CS2046,370.00
651/01/20245CASH IN CS2055,330.00
761/01/20246BANK IN BN20014,710.00
871/01/20247CASH IN CS2034,910.00
981/01/20248BANK IN BN20025,310.00
10TOTAL39,730.00
1112/01/20249CASH IN CS20036,340.00
1222/01/202410CASH IN CS2065,860.00
13TOTAL12,200.00
1413/01/202411CASH IN CS2074,820.00
1523/01/202412BANK IN BN20044,200.00
16TOTAL9,020.00
17
DATE
 
Upvote 0
Here another macro to try:

VBA Code:
Sub Insert_Total()
  Dim a As Variant, b As Variant
  Dim ant As Double, tot As Double
  Dim i As Long, k As Long, n As Long
  
  a = Sheets("RESULT").Range("A1:F" & Sheets("RESULT").Range("C" & Rows.Count).End(3).Row + 1).Value
  ReDim b(1 To UBound(a) * 2, 1 To 5)
  
  ant = a(2, 1)
  For i = 2 To UBound(a)
    If ant <> a(i, 1) Then
      n = 0
      k = k + 1
      b(k, 2) = "TOTAL"
      b(k, 5) = tot
      tot = 0
      If i = UBound(a) Then Exit For
    End If
    n = n + 1
    k = k + 1
    b(k, 1) = n
    b(k, 2) = a(i, 1)
    b(k, 3) = a(i, 2)
    b(k, 4) = a(i, 3)
    b(k, 5) = a(i, 6)
    tot = tot + a(i, 6)
    ant = a(i, 1)
  Next
  
  'FORMAT CELLS
  With Sheets("DATE")
    .Range("A2:E" & Rows.Count).Clear
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    .Range("A:E").HorizontalAlignment = xlCenter
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("E:E").NumberFormat = "#,##0.00"
    .Range("A2", .Range("E" & Rows.Count).End(3)).Borders.LineStyle = xlContinuous
  End With
End Sub

🤗
 
Upvote 0
Solution

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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