Sum rows by ageing

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
890
Hi to everyone, i would like to write a VBA command so that to sum the rows which are contains in column "C" the letters "CRN" & "COR". and are over one year as from today which is in column "F"3 and delete then the rows which are sub totals. In addition the name of Debtor in column 'A" should be turn to one row below so that to be in the same line of the sum. At F.1. are the data before adjusting and in F.2. is the expected result. The rows are change when ever i download the data and the spreadsheet contains more than 30.000 rows. I would be greatly appreciated any help. Thanking you in advance.

ABCD EF
1
2
310/12/2014
4
5Debtor NameDateDocumentDescriptionDebit Credit
6
7Company ABCD
813/3/2010CRN - 13031001CREDIT NOTE 0,001.580,00
925/5/2011CRN- 25051101CREDIT NOTE 0,00620,00
1025/10/2014COR - 125214CORRECTION0,00155,00
11Company EFGH
1225/8/2010CRN - 25081001CREDIT NOTE 0,00150,00
1313/9/2010CRN - 13091002CREDIT NOTE 0,0075,00
1410/8/2014CRN - 10081401CREDIT NOTE 0,00225,00
15
15/9/2014COR - 25090902CORRECTION0,0055,00

<colgroup><col style="mso-width-source:userset;mso-width-alt:2048;width:42pt" width="56"> <col style="mso-width-source:userset;mso-width-alt:3840;width:79pt" width="105"> <col style="mso-width-source:userset;mso-width-alt:3108;width:64pt" width="85"> <col style="mso-width-source:userset;mso-width-alt:4315;width:89pt" width="118"> <col style="mso-width-source:userset;mso-width-alt:3584;width:74pt" width="98"> <col style="mso-width-source:userset;mso-width-alt:3547;width:73pt" width="97"> <col style="mso-width-source:userset;mso-width-alt:4096;width:84pt" width="112"> </colgroup><tbody>
</tbody>




ABC
D EF
1
2
310/12/2014
4Debtor NameDateDocumentDescriptionDebit Credit
5
6Company ABCD13/3/2010CRN - 13031001CREDIT NOTE 0,002.200,00
7
8Company EFGH25/8/2010CRN - 25081001CREDIT NOTE 0,00225,00

<colgroup><col style="mso-width-source:userset;mso-width-alt:2048;width:42pt" width="56"> <col style="mso-width-source:userset;mso-width-alt:3840;width:79pt" width="105"> <col style="mso-width-source:userset;mso-width-alt:3108;width:64pt" width="85"> <col style="mso-width-source:userset;mso-width-alt:4315;width:89pt" width="118"> <col style="mso-width-source:userset;mso-width-alt:3584;width:74pt" width="98"> <col style="mso-width-source:userset;mso-width-alt:3547;width:73pt" width="97"> <col style="mso-width-source:userset;mso-width-alt:4096;width:84pt" width="112"> </colgroup><tbody>
</tbody>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello,

not very elegant, but seems to work (on your sample data anyway).

Code:
Sub COMBINE()
    Application.ScreenUpdating = False
    MY_DATE = Range("F3").Value - 365
    For MY_ROWS = Range("B" & Rows.Count).End(xlUp).Row To 6 Step -1
        If Range("B" & MY_ROWS).Value < MY_DATE And Not (IsEmpty(Range("B" & MY_ROWS - 1).Value)) Then
            If Left(Range("C" & MY_ROWS).Value, 3) = "COR" Or _
                Left(Range("C" & MY_ROWS).Value, 3) = "CRN" Then
                    MY_TOTAL = MY_TOTAL + Range("F" & MY_ROWS).Value
            End If
        Else
            If IsEmpty(Range("B" & MY_ROWS - 1).Value) Then
                Range("B" & MY_ROWS & ":F" & MY_ROWS).Copy
                Range("B" & MY_ROWS - 1).PasteSpecial (xlPasteAll)
                MY_TOTAL = MY_TOTAL + Range("F" & MY_ROWS).Value
                Range("F" & MY_ROWS - 1).Value = MY_TOTAL
                Rows(MY_ROWS).Delete
                MY_ROWS = MY_ROWS - 1
                MY_TOTAL = 0
                GoTo CONT
            End If
        End If
        Rows(MY_ROWS).Delete
CONT:
    Next MY_ROWS
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi onlyadrafter, somehow it seems to work but maybe need to be corrected the function for deleted rows. The VBA code keeps and other transactions types e.g. "INV". When i run the code should be sum for each debtor its transactions which contains in col. "C" the transactions types with letters "CRN" and "COR" and are over 365 days combining the today date in cell "F3" with transaction date in col "B" Any other transactions should be delete entire row. However great thanks for the time you spend for my project.
 
Upvote 0
Hello,

when I ran my code, I got the same results as you posted in your original post.

From what you originally posted, what isn't working correctly.

I have assumed that all the data is laid out the same as your example, i.e. company name is on its own line with the other data beneath.
 
Upvote 0
Hi again, You have right, for my extract i present, it works correctly. Just note that an extract of my data and the whole data when i download them, consists more than 30000 rows. We have to note that in my example just i present only 2 transactions types (CRN & COR). In my actual data there are some more like "INV", "REC", "ADJ" which the code doesn't delete their rows. Also i tested several times and there are CRN or COR which should be keep the rows and are over one year, but the code delete them. Anyway I am shy to bother you or discomfort you any more. Great thanks for the work you done for my project, really i appreciated and it was very kind of you. Thanks once again and i wish you all the best.
 
Upvote 0
Hello,

so you want to keep rows of data that aren't over a year old.

Code:
Sub COMBINE()
    Application.ScreenUpdating = False
    MY_DATE = Range("F3").Value - 365
    For MY_ROWS = Range("B" & Rows.Count).End(xlUp).Row To 6 Step -1
        If Range("B" & MY_ROWS).Value < MY_DATE And Not (IsEmpty(Range("B" & MY_ROWS - 1).Value)) Then
            If Left(Range("C" & MY_ROWS).Value, 3) = "COR" Or _
                Left(Range("C" & MY_ROWS).Value, 3) = "CRN" Then
                    MY_TOTAL = MY_TOTAL + Range("F" & MY_ROWS).Value
                    Rows(MY_ROWS).Delete
            End If
        Else
            If IsEmpty(Range("B" & MY_ROWS - 1).Value) Then
                Range("B" & MY_ROWS & ":F" & MY_ROWS).Copy
                Range("B" & MY_ROWS - 1).PasteSpecial (xlPasteAll)
                MY_TOTAL = MY_TOTAL + Range("F" & MY_ROWS).Value
                Range("F" & MY_ROWS - 1).Value = MY_TOTAL
                Rows(MY_ROWS).Delete
                MY_ROWS = MY_ROWS - 1
                MY_TOTAL = 0
                GoTo CONT
            End If
        End If
CONT:
    Next MY_ROWS
    Application.ScreenUpdating = True
End Sub

This should keep rows of data that are less than a year old, and keeps rows that aren't COR or CRN.
 
Upvote 0
Hi again, No is not exactly what you say. I need the code to keep "CRN and "COR" which are belong to the current year. (are not over a year old). Any other row should be deleted. Thanks once again for your attempt to resolve my matter. All the best.
 
Upvote 0

Forum statistics

Threads
1,221,842
Messages
6,162,333
Members
451,759
Latest member
damav78

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