Excel VBA to count records based on matching dates criteria

aaleem

Board Regular
Joined
Sep 26, 2014
Messages
56
Office Version
  1. 2016
Hi,

currently, I have a sheet of data that contains thousands of records, I would like to match each record and provide the summarized results.
the criteria will be

if the batch date = payment date then it should count the no. of records as Current Dated
if the batch date > payment date then it should count the no. of records as Back Dated
If the batch date < payment date then it should count the no. of records as Post Dated
A sample sheet of data is attached here for reference.

Im currently using the if formula, howe we can achieve the same result in VBA, as the no. of records are voluminous using formula is making the excel slow and increasing its file size.

thanks
Aleem
Book2
ABCDEFGHIJKL
1Payment IDMatch Doc TyPayment/ ItemPayee NumberBank Acct-G/LCheck/ Itm DateVoid DateBatch NumberBth TyBatch Date Payment Amount Status
21PN5266710712786240253900316/05/202152667073M16/05/2021(382,784.14)Current Dated
32PN5266711812610050253900315/05/202152667074M16/05/2021(516,758.59)Back Dated
43PN5266712912763740253900316/05/202152667075M16/05/2021(697,624.10)Current Dated
54PN5266714012484510012209816/05/202152667076M16/05/2021(941,792.53)Current Dated
65PN5266715112762830012209816/05/202152667077M16/05/2021(1,271,419.92)Current Dated
76PN5266716212597580012209831/05/202152667078M16/05/2021(1,716,416.89)Future Dated
87PN5266717312213750012209818/05/202152667079M16/05/2021(2,317,162.81)Future Dated
98PN526671841069070012209816/05/202152667080M16/05/2021(3,128,169.79)Current Dated
109PN5266719512693420012209819/05/202152667081M16/05/2021(4,223,029.22)Future Dated
1110PN5266720612712140012209820/05/202152667082M16/05/2021(5,701,089.44)Future Dated
12
13
14Results should be
15Current Dated Records5which is = 16/05/2021
16Back Dated1which is < 16/05/2021
17Future Dated4> 16/05/2021
18Formula usedIF(J2=F2,"Current Dated",IF(J2>F2,"Back Dated","Future Dated"))
Sheet1
Cell Formulas
RangeFormula
L2:L11L2=IF(J2=F2,"Current Dated",IF(J2>F2,"Back Dated","Future Dated"))
 
Any insight into the three questions above would be welcomed!
Hi @kennypete
I'm not sure that I can add a lot in answering your questions. I have no formal training or knowledge about vba. Almost everything I know about it I picked up in this forum. In particular, in relation to this code I originally got the concept of loading the array that way from this thread, where you might find some of the discussion by pgc01 and Rick Rothstein helpful, including the fact that during loading an array using Application.Index you can alter the order of the columns or rows.

presuming your Worksheet is Sheet1, of course
No, my code was simply assuming the sheet with the data is the active sheet since unqualified range references in a general module will refer to the active sheet.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I'm not sure that I can add a lot in answering your questions. I have no formal training or knowledge about vba. Almost everything I know about it I picked up in this forum. In particular, in relation to this code I originally got the concept of loading the array that way from this thread, where you might find some of the discussion by pgc01 and Rick Rothstein helpful, including the fact that during loading an array using Application.Index you can alter the order of the columns or rows.
Thanks Peter, that's all good.

In terms of Q1, Application.WorksheetFunction.Index versus Application.Index, yes the link referenced in the thread you quoted more-or-less explains it: The WorksheetFunction Method – Daily Dose of Excel

In terms of Q2, "Cells", I did find this: "When you apply the Cells property to a worksheet without specifying an index number, the method returns a Range object that represents all the cells on the worksheet - "Refer to All the Cells on the Worksheet so that answers that question. So, Cells could be replaced with something like Range("A:J") in this instance and the result would be the same. Luckily the Cells call when within that Index function does not exceed memory anyway when limited by the rows and columns details.

  • Incidental: For some light entertainment I found that Dim x: x = Range("A:EZ") sees Excel memory usage jump to nearly 4GB and Dim x: x = Range("A:FZ") gives a Runtime error 7 out of memory. Now I have 32GB RAM so clearly it's not even close to hitting that and Out of memory (Error 7) does not suggest a 4GB limit. Perhaps it's: Large Address Aware capability change for Excel - Office - noting, "the available memory for the Excel process is automatically doubled from 2 GB to 4 GB", though I have 64-bit Office 365 so perhaps it was not even fixed for the 64-bit version.

Finally, as for Q3, Two and one dimensional row and column args: from testing it, the reason for this is that row produces a 2D array whereas column produces 1D. Compare:
VBA Code:
Dim r: r = [row(2:12)]
Dim c: c = [column(F:J)]
1621762313210.png

If row did not behave this way I guess using Array() could work for it too rather than having to use Evaluate/Row, which does seem the quickest way of populating the 2D array (versus using a cluncky Dim r(): ReDim Preserve r(1 To n, 1 To 1): r(1, 1) = 2: r(2, 1) = 3, perhaps with a loop, to populate the 2D array for the rows, which does work).

Anyway, 'twas an interesting one, thanks.
 
Upvote 0
So, Cells could be replaced with something like Range("A:J") in this instance and the result would be the same.
It could be, but Rick's observation in that thread was that there seemed to be no detriment to using Cells - & it is simpler to type. ;)
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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