VBA code for SUMIFS function

Akira181

Board Regular
Joined
Mar 23, 2010
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I'm very new to VBA and I'm currently struggling with how to do a SUMIFS code with a table of data. I can follow along some tutorials and examples to get it to work for a single cell, but struggling when it comes to a full table of data.

I'm trying to find the total cost for each unique reference in Sheet 2 and paste it into the corresponding Week cell for that Ref in Sheet 1. Below is what my data is going to look like:

Sheet 2
The columns with "..." in them have data but aren't relevant for what I'm trying to do. There are potentially an unlimited number of rows and I would like to find the total Cost for each unique reference in Column B

Book1
ABCDEFGH
1
2
3RefCostDateCW
4001£ 1.0002/01/20211
5001£ 2.0007/01/20212
6003£ 3.0012/01/20213
7005£ 4.0012/01/20223
8003£ 5.0013/01/20223
9002£ 6.0005/02/20226 
10003£ 7.0006/02/20226 
11004£ 8.0006/02/20226 
BOM


Sheet 3
This is what I want the end result to look like. I can do it with a formula but it needs to be in VBA. Can someone help a novice?

Book1
ABCDEFGHIJ
1
2MonthJanJanJanJanFeb
3RefWK.12345
4001100100000
50020000100
6003002000100
70040010000
80050000100
Sheet3
Cell Formulas
RangeFormula
F2:J2F2=TEXT(DATE(YEAR(NOW()),1,F3*7-2),"mmm")
F4:J8F4=SUMIFS(BOM!E4:E11,BOM!B4:B11,Sheet3!B4:B8,BOM!G4:G11,Sheet3!F3:J3)
Dynamic array formulas.
 
Glad to be of help. I did realise after I had written the code that it could be slightly more efficient when writing out the results by looping through the refarr first, however if the code works then the lack of perfection is probably irrelevant.
Not a problem, any lack of perfection will definitely go unnoticed. My modifications and Frankenstein smashing of multiple codes together is definitely more inefficient than anything you could do. I'm still getting to grips with the basics :P

Sorry to grave dig this but the turn of the year has raised a bug. Items in the BOM sheet from last year are getting displayed in table in Sheet 3 as future items. As in, Dec 21 items are getting displayed in the table in sheet3 when it should only have this years data in it.

Unfortunately, I cannot change the BOM data as the bulk of it is auto-generated. How difficult would it be to modify the sumifs VBA to ignore any dates that don't match the current year?

The current year is on a different sheet just now but I can put it in F2 for ease
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
you can easily exclude data that is from the previous year by adding an if statement around the bit that loads thie dictionalry like this:
VBA Code:
For i = 4 To lastrow
 If Year(inarr(i, 6)) = Year(Date) Then   'add this line
 
   If dict.exists(inarr(i, 2)) Then
     Erase temparr
     ReDim temparr(1 To cwmax)  ' to pass values to dictionary
     temparr = dict.Item(inarr(i, 2)) ' load dictionary item into temp array
     temparr((inarr(i, 7))) = temparr(inarr(i, 7)) + inarr(i, 5)  ' add cost to total using week no as index
     dict.Item(inarr(i, 2)) = temparr  ' write tempory array back to dictionary
   Else
     Erase temparr
     ReDim temparr(1 To cwmax)  ' to pass values to dictionary
     temparr(inarr(i, 7)) = inarr(i, 5)   ' add first cost into total using week no as index
     dict.Add inarr(i, 2), temparr()
    End If
 End If                                    'addthis line
 Next i
 
Upvote 0
Was determined to figure out myself why it wasn't working yesterday and gave up till this morning. Fresh set of eyes and I realised it was working, I just didn't delete the existing wrong data in the Sheet3 table, 2 hours I won't be getting back ?‍♂️

Bought a VBA for Dummies book, if I can get halfway to your skill level by the end of the year, I'll consider that a success!

Thanks again, a great help as always!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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