Array Function VBA Out of Memory issue

janpol

New Member
Joined
Apr 30, 2019
Messages
6
Hello,
I have a file with records which include transaction dates. I want to grab the last transaction date in every month and assign it as a statement date. I used an array function to implement this and it works. Below is a small sample that illustrates the idea.


The formula in B2 is as follows: ={MAX(IF(MONTH($A$2:$A$5)=MONTH(A2),IF(YEAR($A$2:$A$5)=YEAR(A2),$A$2:$A$5,"")))}

I then used the array function in VBA to automate the process. Since array functions use a lot of memory, I ended up having memory issues with only about 2000 records.
The issues with my approach:
1. Using array functions
2. I don't need to use the whole column to determine the date of the last transaction in every month. The records are sorted, I only need to go up to the last date every month.

I think I should use a traditional loop in VBA to accomplish this. Any hint is appreciated.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Maybe this will work for you (change Sheet name accordingly):

Code:
Sub foo()
    Dim lngLastRow          As Long
    Dim lngACount           As Long
    Dim lngBCount           As Long
    Dim lngMaxDate          As Long
    
    With [COLOR=#ff0000]Sheet2[/COLOR]
        lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For lngBCount = 2 To lngLastRow
            lngMaxDate = .Range("A" & lngBCount).Value
            For lngACount = 2 To lngLastRow
                If Month(.Range("A" & lngACount).Value) = Month(lngMaxDate) _
                    And Year(.Range("A" & lngACount).Value) = Year(lngMaxDate) _
                    And Day(.Range("A" & lngACount).Value) > Day(lngMaxDate) Then
                    lngMaxDate = .Range("A" & lngACount).Value
                End If
            Next lngACount
            .Range("B" & lngBCount).Value = lngMaxDate
        Next lngBCount
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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