I am trying to sum a row biased on the fact that another row has the same month as a variable that is defined by the user. I believe the sumif function would be my best bet but it doesn’t seem to be working for me. The row I want it to look at obviously is a row of Date with date formatting (used .numberformat = “ddd mm/dd”) but the sumif doesn’t seem to be able to pick out the month in each cell. Here is the code I have so far:
Dim cMade As Integer
Dim sDate As Date
cMade = cMade + Application.WorksheetFunction.SumIf(ActiveSheet.Range("B1:RC1"), Month(sDate), ActiveSheet.Range("B30:RC30"))
As you can see sDate is the date that the user imputs via userform then i convert that using dateserial. and B1-RC1 are the dates i want it to look at. And Range B30-RC30 are the sums i want it to grab if the months match up (the reason I have it as + cMade is because it is in a loop that loops through sheets).
Is this code correct? I have been messing with it and looking for alternatives for hours but I cant not seem to come up with anything!
Assuming this is only a part of your code and I may only guess the rest, try the following corrections:
Range("B30,RC30")asRange("B30:RC30")– comma here looks like a typo according to the description.Application.SumIfwithApplication.WorksheetFunction.SumIf.As an addition, consider to define ranges more precise, e.g. via
Worksheets("Sheet1").Range("A1:C10")orActiveSheet.Range("A1:C10")syntax. However, this may be an extra measure – I don’t see the rest of your code.Hope that was somehow helpful.