SumIf using dates and ranges in VBA - excel

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:
I think you should define Range("B30,RC30") as Range("B30:RC30") - comma here looks like a typo according to the description.
Replace Application.SumIf with Application.WorksheetFunction.SumIf.
As an addition, consider to define ranges more precise, e.g. via Worksheets("Sheet1").Range("A1:C10") or ActiveSheet.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.

Related

How to automatically change reference based on variable in excel

So I'm trying to create a Forecast using historic data from 2 years. Each year's data is broken down into weeks and weeks that haven't occurred yet are set to 0. I'm struggling trying to create a formula that will automatically run a Forecast on only the weeks in the year that have occurred. I created this formula which Excel won't execute:
=UNIQUE(FILTER(WkSht!G:(VLOOKUP(F1,DH2:DI54,2)),(WkSht!A:A)=(B1)))
I'm trying to use VLOOKUP to replace the second part of a cell reference based off a lookup table. So if F1 is 25, for example, then the Filter function will be:
=UNIQUE(FILTER(WkSht!G:AE,(WkSht!A:A)=(B1))
That second formula works on its own as intended, but I'm trying to create this excel file so that it requires minimal work to update in the future and manually changing the range seems like a bit too much work to expect other people to do.
So I guess my question is:
How do I change part of the reference automatically?
Maybe I could do:
=UNIQUE(FILTER(VLOOKUP(F1,DH2:DI54,2)),(WkSht!A:A)=(B1)))
And have to lookup values contain the reference text?
Alternatively, is there a way to filter out the last of the 0's in the FORECAST.ETS function (as some values might intentionally be 0s in earlier weeks)?
To get a variable width range, you can use the construct
SomeRange:INDEX(MaxRange,,NumberOfColumns)
In your case SomeRange would be
WkSht!G:G
MaxRange would be something like
WkSht!G:Z
where you replace Z with a column that comfortably covers all your (future) data
NumberOfColumns is your VLookup (You probably want an exact match?, If so include the 4th parameter =0)
Demo:
=UNIQUE(FILTER(WkSht!G:G:INDEX(WkSht!G:Z,,VLOOKUP(F1,DH2:DI54,2,0)),WkSht!A:A=B1))
Sample data on sheet WkSht

Using a date range in an array formula

I've got a data-set, for example purposes it's a list of people (who may appear more than once) with various fields.
I need to return every match and I've done this with an array formula.
My data looks like this from Column A to F with the look up result shown in Column F. (I've simplified it for sample purposes):
and the data updates perfectly when i update the name in cell F1
This is my formula
{=IFERROR(INDEX($C$2:$C$27, SMALL(IF(COUNTIF(F$1,$A$2:$A$27), ROW($C$2:$C$27)-1,""), ROW()-1)),"")}
However I need to also to include the date in the criteria, specifically I want to be able to provide a date range, and then only return matches that are within that date range.
I've found many examples of having multiple matches in the criteria (using more than 1 countif and multiplying the results together) but nothing that will allow me to specify a date range.
I don't want to use VBA for this, hoping someone out there can assist.
always the case.
After posting that query, I managed to work it out myself.
I just changed my formula to :
{=IFERROR(INDEX($C$2:$C$27, SMALL(IF(COUNTIF(F$1,$A$2:$A$27)*($G$1>=$B$2:$B$27), ROW($C$2:$C$27)-1,""), ROW()-1)),"")}
(where G1 had a date in it) i also added a <= statement in there as well to give it a start and end date
sorry for wasting the time of anyone who looked into this for me

Countifs formula: count if matches month, year and another criteria

I'm sure this is quite a simple formula to some, however I'm struggling to get this right.
The data I have is:
A C D
Location 1 Name 10/10/2019
Location 2 Name 01/10/2019
Location 1 Name 10/08/2020
Location 1 Name 12/10/2019
. . .
I am trying to count how many are in a location in each month of the year. (Eg above the count for Location 1 in Oct 19 is 2)
I have managed to get some aspects working, but I cannot get it working together.
eg, this works: =COUNTIFS('sheet1'!D5:D298,">"&30/10/2020)
works however =COUNTIFS('sheet1'!D5:D298,"<"&30/10/2020) does not.
I also cannot get it working with the multiple criteria. ( =COUNTIFS('Counter Balance'!D5:D298,">"&30/10/2020, 'Counter Balance'!A5:A110, "Location 1")
I'm clearly doing something obvious wrong but I just can't see it!
Hopefully that makes sense, any advice would be much appreciate,
thank you!
=COUNTIFS($A$2:$A$5,A9,$C$2:$C$5,">="&DATE(C9,B9,1),$C$2:$C$5,"<"&DATE(C9,B9+1,1))
qty results to 2, as you may expect
The COUNTIFS function needs to have a bit of uniformity in the formula to work.
Microsoft references it here:
Important: Each additional range must have the same number of rows and columns as the criteria_range1 argument. The ranges do not have to be adjacent to each other.
While not necessary, I would recommend using DATE functions, cell references, named ranges, or some other form of control for comparing dates in formulas. Excel treats a date as an integer internally. Combine that with differences in language localization and its pretty easy for the dates in date format to not mean what you want them to mean.
To try to correct your formula, I would recommend this:
=COUNTIFS('Counter Balance'!D5:D298,">"&DATE(2020,10,30), 'Counter Balance'!A5:A298, "Location 1")

Sum up values in excel based on multiple conditions

I have an excel worksheet which captures hours spent by resources for tasks for each work day. The input sheet looks like this -
I need to pull a report based on this data to gather number of hours filled by each resource on each day between a date range (specified by a start and end date). Sample output looks like this -
I have tried using SUMIFS but it fails to give me an output as the row/column range for all conditions and the sum range needs to stay same, which is not same for my case.
Can someone suggest an alternative to get required data?
SUMIF will work for you but you need a dynamic sum range.
This bit can help you achieve that:
=SUMIF($A$2:$A$9,$A15,INDIRECT(CHAR(64+MATCH(B$14,$A$1:$G$1,0))&"2:"&CHAR(64+MATCH(B$14,$A$1:$G$1,0))&"8"))
Most important part is CHAR(64+MATCH(B$14,$A$1:$G$1,0)) which searches position of your date and converts it into column name.
I used 2 and 8 as indicator for the beginning and end of sum range and wrapped everything with INDIRECT to convert string to cell reference.

date dependent cell reference

I was hoping someone might be able to point me in the right direction, I'm trying to automate a section of a spread sheet which shows a persons planned hours of work against the hours they actually did. The data is sourced from a worksheet which is broken down into months and departments.
Is there a way to get the reference table on the main dashboard to vlookup/index a certain column dependent on the computers date?
I.E. for May it will look in the planned and actual hours column for may and then in June it will search the June column for the same data.
Normally I would post some code but I'm not even sure where to begin on this one.
You mention VLOOKUP in you question.
To use this in VBA use, WorksheetFunction.Vlookup(), but it seems you could avoid VBA altogether if you wanted. Here are some formulas of interest:
If you want to search months by the current date, you can use this kind of vlookup (or MATCH() or HLOOKUP() if these are columns as you indicate)
=VLOOKUP(TEXT(TODAY(),"mmmm"),Range,1,FALSE)
Use "mmm" if you use 3-letter months
If months are in static column number can be taken from using the MONTH() function.
=MONTH(TODAY())
"index a certain column dependent on the computers date?"
=TODAY() gets computer's date.
I would tend to use MATCH() to get the months column (in this example I've assumed months are in row 1)
=MATCH(TEXT(TODAY(),"mmmm"),1:1,0)
You can then use INDIRECT() to match a specific month column and department row (if in a 2D format), or SUMIFS() with INDIRECT() if you want to sum hours.
As in:
=INDIRECT("'Sheet'!R" & MATCH("department_name",A:A,0) & "C" & MONTH(TODAY()), FALSE)
Thank you for all the help in regards to this problem, unfortunatly I could not quite get my head round the suggestions which have been put forward, though I have been able to solve this problem (although it is a messy solution)
=IF($F$95="March",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,2,FALSE),
IF($F$95="April",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,3,FALSE),
IF($F$95="May",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,4,FALSE),
IF($F$95="June",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,5,FALSE),
IF($F$95="July",VLOOKUP(D97,'Planned Hours'!$A$1:K37,6,FALSE),
IF($F$95="August",VLOOKUP(D97,'Planned Hours'!$A$1:K37,7,FALSE),
IF($F$95="September",VLOOKUP(D97,'Planned Hours'!$A$1:K37,8,FALSE),
IF($F$95="October",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,9,FALSE),
IF($F$95="November",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,10,FALSE),
IF($F$95="December",VLOOKUP(D97,'Planned Hours'!$A$1:$K$37,11,FALSE)))))))))))
But it works. Again thanks to all that lent a hand.
--EDIT--
By the way this is not in VBA in the end.

Resources