Is there any way to COUNTIF it's the current date?
For example I have a spreadsheet with work orders, once the employee starts the work order it captures a time stamp, after the work order is completed it is moved to an archive, well I wanted to create a summary sheet that tells me how many orders we have done for that date, the format of the time stamp is:
2/19/2014 17:10:20
So basically I need a COUNTIF to count the column for the current date.
Is this possible?
The new version of Google Sheets has COUNTIFS, which would allow =COUNTIFS(A:A,">="&TODAY(),A:A,"<"&TODAY()+1)
You need to opt in to the new version to make it work, though. ("Try the new Google Sheets")
You would need to apply a function first on the range, which then makes COUNTIF not an appropriate function to count those dates matching 'today'. You can use SUMPRODUCT instead:
=arrayformula(SUMPRODUCT(1*(INT(A1:A100)=TODAY())))
INT strips out the time from the datetime.
If you don't have COUNTIFS available, as per maybeWeCouldStealAVan's suggestion, then you can use two COUNTIF functions like this
=COUNTIF(A:A,">="&TODAY())-COUNTIF(A:A,">="&TODAY()+1)
by counting all entries greater than or equal to today then subtracting all those greater than or equal to tomorrow the result is a count of those on today's date only
That formula will work in Excel or google - another option, similar to Jerry's is to use this googledocs specific formula
=count(filter(A1:A100,int(A1:A100)=today()))
try this:
dim i as integer
dim objDate as date
dim objStartDate as Date
dim objEndDate as Date
dim countOrders as integer
countOrders = 1
objStartDate = 'the start date to check with ( could be the start of the day)
objEndDate = 'the end date to check with (could be the end of the day)
for i = 1 to 'number of rows
objDate = CDate(cells(i, 1))
if (objDate > objStartDate) and (objDate < objEndDate) then
countOrders = countOrder +1
end if
next i
Related
I am looking up a price multiplier for a given date in an Excel sheet, but range.find gives the wrong date. Here are the relevant code snippets:
If Not IsWbOpen("daily_prices.xlsx") Then
Workbooks.Open "C:\.....\daily_prices.xlsx"
End If
Set daily = Workbooks("daily_prices.xlsx").Sheets(1)
daily.Range("A:A").NumberFormat = "dd/mm/yyyy"
End If
This part makes sure that the column A:A, which contains the dates, is formatted correctly.
The code then loops through a date range where d is a date between d1 and d2. In my test script, d1= Jan 1, 2023 (formatted as "01/01/2023"), and d2 = Jan 30, 2023 (formatted as "30/01/2023").
The Excel sheet "daily" has a header in row 1 and 49 different dates in column A, ordered chronologically from "11/01/2023" to "01/01/2024", and the related multiplier (k), starting in row 2.
Only the dates that have a multiplier defined are present in the sheet. The first group of dates is from "11/01/2023" at row 2 to "22/01/2023" at row 13, the next starts with "17/02/2023" at row 14, and so on. Dates in between have no multiplier, and thus are not present.
I search the multiplier with this part of the code:
Set c = daily.Range("A:A").Find(what:=d, After:=Range("A1"), LookIn:=xlValues) 'is this date in the daily prices?
If Not c Is Nothing Then
k = CDec(daily.Cells(c.Row, 4)) ' multiplier is in column 4 (D:D)
rate = rate * k
End If
As expected, c is NOTHING for all dates below "11/01/2023". When d = "11/01/2023", c becomes "not NOTHING" but strangely it returns "11/11/2023" instead of "11/01/2023", which is in row 40 instead of row 2, thus it fetches the multiplier of row 40 instead of row 2.
I have re-checked all the code after reading several similar cases and made sure that column A is formatted correctly as "date", but I still get "11/01/2023" wrongly 'found' at row 40 instead of row 2, and the value returned is "11/11/2023" instead of "11/01/2023".
Instead of "12/01/2023", it finds "31/12/2023", and instead of "14/01/2023", which is part of the dataset, it finds nothing.
What did I overlook, where is the error?
Eventually, I found the solution.
It appears that using dates formatted as dates leads to ambiguity with Range.Find(d) as it's very similar to a text search, and depending what date format you use, their partial strings (month and day) may create confusion in certain language versions of Windows. I noticed this when I was looking for a date with day=11 or 12 and found a date with month=11 or 12. This is typical for the confusion that occurs sometimes when you use data formats like "mm-dd-yyyy" (US) and "dd/mm/yyyy" (other) and looks like a Windows bug to me. Windows should distinguish the two formats clearly, but sometimes it doesn't.
There was a similar problem at VBA Range.Find method not finding a value that IS in the range that helped me find the solution.
I needed to format the search object temporarily as numbers
daily.Range("A:A").NumberFormat = "0"
and also search for the long integer value of the running date, thus the starting value is
d = CLng(date1)
where d is type number and date1 is type date. The search term is then simply
Set c = daily.Range("A:A").Find(d)
If Not c Is Nothing Then ...
At the end I reformat the date column with
daily.Range("A:A").NumberFormat = "dd/mm/yyyy;#"
This resolved the problem.
Thank you all for your kind help.
#Ron Rosenfeld's hint came closest as he proposed to search for What:=CDbl(date), which is what I am basically doing, except that I prefer CLng over CDbl as I need the date as an integer and not as a floating point number. The only problem with that idea was that you cannot search for .value2. You can only search for xlValues (which is not the same) or xlFormulas. Neither would find this long integer number in the cell's properties.
Transforming both the search object and the search item to numbers resolves the ambiguity completely.
I have a form that is supposed to show the total sum of different numeric values between two dates, a start date and a finish date. For this, I thought the best option would be to use the SumIfs WorkSheetFunction. However, after trying to code this, the function is not working properly. I am not sure of what is wrong. If I type the exact same formula on the worksheet that has the table with my sample data, it works perfectly.
So, the form I designed is the following:
A second label and textbox will be added for the finish (or end) date. However, I thought it would be better to do that once I get the code to work with a single date in the beginning. The textbox where the user will insert the start date is called tbxDate and the textbox that will show the resulting sum is called tbxBalance. The button that triggers the SumIfs functions is called cmdCalculate.
Also, the table that stores the data (which only has one row of data so far for testing purposes) is this one:
The table name is Sales, and the worksheet name is SalesWS. I thought the code should be pretty simple, unless there is something I am missing. What I did was:
Private Sub cmdCalculate_Click()
Set SalesRange = Worksheets("SalesWS").Range("Sales[TOTAL]")
Set DatesRange = Worksheets("SalesWS").Range("Sales[DATE]")
tbxBalance = Application.WorksheetFunction.SumIfs(SalesRange , DatesRange , ">=" & tbxDate)
End Sub
The issue is that the >= part of the criteria is failing. I only get proper results using only the greater than or less than conditions. For example, if I enter the date 09/08/2020 in the textbox the result in the balance textbox is 0, but if I enter the date 08/08/2020 or anything before it works. It just ignores the condition to sum the values if the date is equal to what is entered. It only works with dates greater or less than what the user inputs in the textbox, excluding the chosen date.
I already checked that the column with the dates in the table is formatted properly.
The version of your code given below should work provided your DATE range contains true dates and tbxDate contains a string VBA can recognise as a date.
Private Sub cmdCalculate_Click()
Dim Fun As Double
Set SalesRange = Worksheets("SalesWS").Range("Sales[TOTAL]")
Set DatesRange = Worksheets("SalesWS").Range("Sales[DATE]")
Fun = Application.WorksheetFunction.SumIfs(SalesRange, DatesRange, ">=" & CLng(CDate(tbxDate.Value)))
tbxBalance = Format(Fun, "#,##0.00")
End Sub
Remember that tbxBalance will cotnain a text string, not a number. Use Excel's NUMBERVALUE function to convert the formatted number you have in tbxBalance after the above code back to a number you can calculate with.
I have a date like this (mm/yy) in row 1
A B C D E F
1/19 2/19 3/19 4/19 5/19 6/19 ...
I want the VBA to recognize today's date and match it to current column and return as integer.
Ignoring the days (only matching month and year).
For example, if today is 4/13/2019, it would be 4 (column D)
I would need this in VBA because I will be using it to define a range:
For today To x month
It appears OP was looking for a VBA solution, so here is an alternative.
I can think of a few completely different methods of accomplishing this within VBA. Your question isn't very clear of what you are wanting the end result to be, but it appears you are looking for a function that will return the column number - perhaps so you can use to pinpoint a range.
Function DateCol(ByVal InputDate As Date) As Long
Dim colDate As Date
colDate = InputDate - Day(InputDate) + 1
Dim srcRng As Range
Set srcRng = ThisWorkbook.Worksheets("Sheet1").Rows(1)
DateCol = srcRng.Find(What:=colDate, LookAt:=xlWhole).Column
End Function
You simply take an input date, subtract the days (and add 1 since the first day of the month isn't 0). Then you take this new date and use the .Find() method to locate the range that contains your date on the worksheet, and finally the .Column property to get the number you are looking for.
Here is a small sample usage:
Sub test()
' Example Usage
Cells(10, DateCol(#6/11/2019#)).Value = "Test"
End Sub
In the above test sub, the DateCol() function would have returned the value of 6 in your sample worksheet, making the result:
Cells(10, 6).Value = "Test"
Only issue is that this function doesn't contain any error handling. You will raise an error if the date is not found in .Find(), so ensure that you take this into consideration.
Also, don't forget to change this line to use the true Worksheet:
ThisWorkbook.Worksheets("Sheet1").Rows(1)
I had to redo my answer after messing with the data. this is what i ended up with.
On Row 1 I entered the dates as: 1/1/2019, 2/1/2019, 3/1/2019... and custom formatted the row to only show it as mm/yy.
With the formula below I grab the month and year from the given date and convert it into the first of the month. I am very positive there is a better way to make it but my brain is fried for the day.
=MATCH(NUMBERVALUE(TEXT(A3,"mm")&"/1/"&TEXT(A3,"yy")),$1:$1,0)
Edit: (Edit formula to make it permanent on Row 1 [$1:$1])
Assuming that the date 4/13/2019 is on Cell A3
I am trying to simplify a process of counting how many of a specified criteria are in a table.
What I need is to count the number of items that meet all of the following criteria:
[BusinessArea] = "Corporate"
[Application] = "CS"
[Status] = "Resolved"
[ResolvedDate] = *if the resolved date is between DateA and DateB eon a
separate worksheet.
I can do it using VBA or Formulas but I just cannot figure out the date part. I have them figured out separately as:
=IF(AND(Sheet1!I71 >= (TODAY()-7), Sheet1!I71 <TODAY()), TRUE, FALSE)
Where i71 is the [ResolvedDate] (it is searching just this one entry without the other filters.
=COUNTIFS(Table8[Business Area], "Corporate", Table8[Application], "CS")
Where it counts the number of entries that are Corporate_CS entries.
What I currently have:
'=COUNTIFS(Table8[Reported Date],AND(Table8[Reported Date]<='Ticket Summary'!F61, Table8[Reported Date]< TODAY()),Table8[Business Area], "Corporate", Table8[Application], "CS")'
Where F61 is a previous date (beginning of range)
Of which it is returning 9 instead of 6. There are 9 entries that match the criteria, 6 matching the date range and criteria
Try this:
=COUNTIFS(Table8[BusinessArea], "Corporate", Table8[Application], "CS",Table8[Status], "Resolved", Table8[ResolvedDate], ">=" & I61, Table8[ResolvedDate], "< " & I71)
I'm assuming that I61 is the start date and I71 is the end date.
I used this as a guide for the dates: count-cells-between-dates
Multi-criteria counting is a little tough in Excel. I used to make extra columns and concatenate values. Then you can use countif without too much trouble. But it was messy. You could also use DCOUNTA by setting up your query parameters on another part of your sheet (but it doesn't like tables - only cell ranges). Again, its a little messy, but it's very flexible.
My preferred method is to use the SUMPRODUCT function. Everything can be done in one formula and it works with tables. To get your count using SUMPRODUCT:
=SUMPRODUCT((Table8[BusinessArea]="Corporate")*(Table8[Application]="CS")*(Table8[Status]="Resolved")*(Table8[ResolvedDate]>DateA)*(Table8[ResolvedDate]<DateB))
I have a refreshable table in excel and I want to filter the rows by a couple of date ranges. Each row has a date and other information.
I want to find the rows that are in the first date range (F1:F2) and are not in the second date range (H1:H2).
The table is refreshable, and can change size. It currently spans A3:X6146. The table is a query, so it will change sizes when a separate date range is used to find the table values.
I don't have much VBA experience, so this problem is tripping me up. Any ideas?
Thanks
EDIT:
I'll try to make the issue clearer.
I have a table that is created via a query that pulls in data that falls between the Starting Date and the Ending Date, 1/1/2016 and 12/31/2017 here. It lists each time an item was purchased, so each one can be listed multiple times.
I want to find which items were purchased (listed in the table) between the Active Date Range start and end dates (cells F1 and F2), and NOT purchased between the Inactive Date range (cells H1 and H2).
Starting Date: 1/1/2016 Active Date Range Start: 3/1/2016 Inactive Date Start: 3/2/2017
Ending Date: 12/31/2017 Active Date Range End: 3/1/2017 Inactive Date End: 9/22/2017
item date
1 9/21/2017
2 9/20/2017
3 9/20/2017
Yes, I can say to you what I would do.
Create one additional column to keep the result value, if is in or out of the date range. Then
dim t() as string, lin as long, linf as long
linf=Range("F65536").End(xlUp).Row 'or any other more precise way to get the final line
redim t(1 to linf-2,1 to 1)
'range dates - are they on the worksheet?
dim rg_dates as Range,r as range,b as boolean
set rg_dates=Sheets("xxxx").Range("B1:B4") ' just an example, the range would be B1:C4 but use only the first column - see below
For lin=3 to linf
b=False
For each r in rg_dates
If cells(lin,"F").value>= r.Cells(1,1) and cells(lin,"G").Value<=r.Cells(1,2).value then
b=true
Exit for
End If
Next r
If b then t(lin-2,1)="Y" else t(lin-2,1)="N"
Next l
Range("Z3:Z" & linf).Value = T
'Then just filter the table.
There would be then many things to do to keep it error free, and how to apply it at the concrete situation. Hopefully with what I wrote above you can get an idea about things you can do using VBA. If you are using code to filter the table you can do all this invisible to the user, creating an extra column for the filter criteria, filtering, and then deleting the whole column..