Transforming formula to VBA Code

Newbie73

Board Regular
Joined
Feb 4, 2024
Messages
109
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to transform some formulas into VBA code to make the Excel spreadsheet more efficient as it contains thousands of data.

Is there a way to do the calculation in the provided example spreadsheet by code?

The formula itself works as intended, from column A selects one ID per year based on the date from Column C. If for the same ID there's more than one data entry for the same year, sum those entries values of Column B and if in that year for that ID column D ="ir" or "r" then count as 1 (or sum if more than one entry for the same year)

Example spreadsheet:

test11.xlsx

Let me know if I didn't explain well or if you need any more information, thanks!
 
If you put an empty table in your template, it will expand as you add data to it.

It won't make any difference to that code since it just uses the current region of data (if you have any completely blank rows within your data, it won't process all of it, but presumably your data isn't that bad?)
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Thanks I'm looking into it! Bit off topic, but how would then I add the table section into formulas? Got plenty to update if I go that way! For example:

Excel Formula:
=LET(src,Data!A2:M56739, fsrc,FILTER(src,INDEX(src,,1)<>""), shortID,INDEX(fsrc,,1), dates,INDEX(fsrc,,12), maxdate,MAX(dates),
uidmd,SORT(UNIQUE(FILTER(shortID,dates=maxdate))), uidnmd,SORT(UNIQUE(VSTACK(shortID,uidmd),,1)),
utd,FILTER(fsrc,ISNUMBER(MATCH(shortID,uidmd,0))), nutd,FILTER(fsrc,NOT(ISNUMBER(MATCH(shortID,uidmd,0)))),
VSTACK(SORT(utd,{1,12},{1,-1}),EXPAND("",,COLUMNS(fsrc),""),VSTACK(SORT(nutd,{1,12},{1,-1}))))

If I make a table and named Data or whatever, how would I update that formula?
 
Upvote 0
Alex sorry I wasn't clear on that, but to be honest it doesn't actually matter to the real data as those events will only happen maximum of twice in a year, so both sum or count would lead to the same result in this case.
So, I read it wrong...
If IR with 0 happens, it calculates wrong...
Please replace 2 lines of a(i, 2) with 1.
1)
Rich (BB code):
                a(.Count, 3) = IIf(a(i, 4) = "IR", 1, 0)  '<--- 1 was a(i,2)
2)
Rich (BB code):
                If a(i, 4) = "IR" Then a(.Item(s), 3) = a(.Item(s), 3) + 1  <--- 1 was a(i,2)
 
Upvote 0
You mean if IR on column D and 0 on column B? That will never happen either, but a same ID can have IR in two different dates but in the same year (or IR and R). In this case shall I still replace the code lines?

Speaking of it just noticed that "IR" is in the code but not "R". I will do some further testing during the weekend with more time
 
Upvote 0
Just did more testing, the only thing I would still need is to add the "R" alongside the "IR" with exactly the same effect. I've mentioned on my topic but then to be fair forgot to add one in the example sheet. I've added it now on the 2nd row. Is it a matter of just adding or "R" or more complicated?

Thanks!
 
Upvote 0
Do you wnat the count of R add to the same output column or add a new column for R in the result?
Do you have anything other than CLEAR, R or IR?
 
Upvote 0
Same output column as "IR". It should only be those 3, if anything else likely an error/mistake on the data
 
Upvote 0
Use one of the marked lines that you prefer.
Rich (BB code):
Sub test()
    Dim a, i&, y&, s$
    a = [a1].CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(a, 1)
            y = Year(a(i, 3))
            s = Join(Array(a(i, 1), y), Chr(2))
            If Not .exists(s) Then
                .Item(s) = .Count + 1
                a(.Count, 1) = a(i, 1)
                a(.Count, 2) = a(i, 2)
                a(.Count, 3) = IIf(a(i, 4) <> "CLEAR", a(i, 2), 0)  '<--- this line for sum
                'a(.Count, 3) = IIf(a(i, 4) <> "CLEAR", 1, 0)  '<--- this oine for count
                a(.Count, 4) = y
            Else
                a(.Item(s), 2) = a(.Item(s), 2) + a(i, 2)
                If a(i, 4) <> "CLEAR" Then a(.Item(s), 3) = a(.Item(s), 3) + a(i, 2)  '<--- this line for sum
                'If a(i, 4) <> "CLEAR" Then a(.Item(s), 3) = a(.Item(s), 3) + 1   '<--- this oine for count
            End If
        Next
        [i2].Resize(.Count, 4) = a
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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