Excel VBA Format(<Date>, "mmmmm-yy") not working as expected - excel

I have cells in my excel sheet that contain a date.
The display format for these cells is a custom format "mmmmm-yy"
so that the date "7/1/16" appears as "S-16". This works format works on the excel sheet, however, in VBA I try to call the Format() function on these cells, it does not yield the same format.
Format( <Date_Cell> , "mmmmm-yy")
Gives
"April4-16"
on the chart, for example.
Why does the Format function not behave the same as formatting a date cell?
Edit: According to the office support website, Format a date the way you want, I am using the correct format, but it is not producing the result the website claims it should.
Edit2: Turns out cell date formatting works differently than the VBA Format function, as the selected answer points out.

The meaning of the format symbols of the VBA function Format and the Excel Date Format are not 100% identically.
You could use the worksheet function TEXT like:
Application.Text( <Date_Cell> , "mmmmm-yy")
to achieve what you want.

The VBA Format function doesn't support the "mmmmm" format as first letter of the month (see the documentation for supported formats). Your output is actually parsing as "mmmm" (month name), then "m" (month number). If you only need the first letter of the month, you'll need to parse it out manually:
Left$(MonthName(Month(<Date_Cell>)), 1) & "-" & Right$(CStr(Year(<Date_Cell>)), 2)

One more option:
Change the number format of the cell:
Range("A1").NumberFormat = "mmmmm-yy"

Related

Comparing dates coming from different files in Excel

I am trying to link two Excel files and compare the values from first one in the second one, but I have a problem at some point.
So the date 28/07/2021 comes from the other file (which is call 3WLA). I had to use the function TEXT(..., "dd/mm/yyyy") otherwise it displays 44405 which is an other form of the date. Now I want to compare this date with other ones but it doesn't work as showed in the pictures below.
The picture shows the comparison of two dates coming from the other excel file 3WLA (still using the function TEXT(..., "dd/mm/yyyy")). We clearly see that there is a problem as it should display TRUE. In the same way, when I compare a date from 3WLA (i.e. using TEXT(..., "dd/mm/yyyy")) with a date random in my file it give me the following
where 28/07/2021 is given by = TEXT('[3WLA.xlsx]Block-First-Fix'!$AM$1,"dd/mm/yyyy") and 29/07/2021 by = 29/07/2021
chris neilsen already gave you the answer in the comments.
To clarify further: The text "29/07/2021" and the date formatted to look like "29/07/2021" are different data types. To help with this MS Excel automatically formats numbers and dates to be aligned to the right and normal text to be aligned to the left
In B1 you can see that the date has been formatted in the same way as per the formula in A2, but since the output in B2 is of the text data type, the two are not considered the same.
You can use the formula DATEVALUE to convert from text to date serial number. (Read more about date serial numbers here).
If your texts with dates are too different from your local settings date formats, DATEVALUE might not give you correct results. In that case use DATE function in combination with some text functions such as LEFT, MID, RIGHT
You can format the cell to make the date more readable using either the number formatting field or the dropdown from the ribbon

Date incorrectly converts into required format (dd/mm/yy) in excel

Hi I know lots of similar questions can be found here, but I am still facing this problem:
I have a table with one column called Dates with some values shown below:
12/2/2021
11/2/2021
10/2/2021
9/2/2021
8/2/2021
7/2/2021
6/2/2021
5/2/2021
4/2/2021
3/2/2021
2/2/2021
1/2/2021
31/01/21
30/01/21
When I try to convert it using Format cells either by selecting category as Date or adding custom category as dd/mm/yy, excel treat the dd/mm/YYYY values as mm/dd/yy.
So for example, instead of treating 1st row as 12 February, excel thinks of it as 2 December.
I also tried convert text to columns by going to DATA and setting it as DMY but it doesn't solve the problem.
Any help is really appreciated.
Try this formula:
= DATE( YEAR(A1), DAY(A1), MONTH(A1) )
If the values have any space character, try this one:
= DATE( YEAR(TRIM(A1)), DAY(TRIM(A1)), MONTH(TRIM(A1)) )
Assumptions:
All the dates were originally DMY
Your windows regional settings short date format is MDY
You will need to convert to a proper date
If the value is text, then it will be in DMY format and you can extract the sections and convert to a date
If the value is numeric, then it has been converted improperly and you need to reverse the month and day part
Given those assumptions, the following formula should work.
I am using the LET and FILTERXML functions which are available in Excel Office 365 Windows versions.
If you don't have that version of Excel, you can still do this with formulas but it will be more complex. Or you can use VBA.
=IFERROR(DATE(YEAR(A1),DAY(A1),MONTH(A1)),
LET(x,FILTERXML("<t><s>" & SUBSTITUTE(A1,"/","</s><s>")& "</s></t>","//s"),
DATE(INDEX(x,3)+IF(INDEX(x,3)<30,2000),INDEX(x,2),INDEX(x,1))))

Using a CDate in a Countifs function

I was wondering if there was a way to use the CDate function in a countif statement? I'm trying to find dates less than 2 weeks old, am and currently using this statement:
=COUNTIF(CDATE(Table1[Date Closed]),">=" &TODAY() - 14)
I need to turn the Date Closed of the table into actual dates without changing the table (right now they're in the format 13-Mar-2018 but as text format).
Is there a way to do this within the Countif function or am I going to have to do a a VBA code to first change the dates?
Thank you!
=SUMPRODUCT(--(DATEVALUE(Table1[TextDates])>=(TODAY()-14)))
Although I do wonder why you can't have dates as real Excel dates, rather than text strings
Set up your table and insert a column (you could hide the original or place the new column at the end or next to the original) and convert the text date to a date via the DATEVALUE() function.
See reference:
https://support.office.com/en-us/article/datevalue-function-df8b07d4-7761-4a93-bc33-b7471bbff252
You may want to apply format to the new cell/column to look like a date.
Set up your table for "Using structured references with Excel tables."
See reference:
https://support.office.com/en-us/article/using-structured-references-with-excel-tables-f5ed2452-2337-4f71-bed3-c8ae6d2b276e
Count dates based on your condition.
See reference:
https://support.office.com/en-us/article/count-numbers-or-dates-based-on-a-condition-976d0074-245d-49e6-bf5f-1207983f82ed

Excel DateValue = Formatting

Looking to convert the following 2015-10-07T23:59:59 into something that Excel actually recognizes as a date so I can then graph/chart with this data... I tried using datevalue and custom formatting but can't get the syntax quite right.
To get datetime from your string, you only need to replace T with space, convert to numeric and apply appropriate cell format.:
=1*SUBSTITUTE(A1,"T"," ")
I dont know if Excel can understand that type of format date, you should try to write your own custom function to get it in the format that you want.
How about yyyy-mm-ddThh:mm:ss for the custom format?
If the timestamp isn't needed, use =DATEVALUE(LEFT(A1,10)). If it is needed, then use =DATEVALUE(LEFT(A1,10))+TIMEVALUE(RIGHT(A1,8)). (Where cell A1 contains the datetime string you are trying to parse).
This will return the Julian date value (E.g. 42284 for the day, 42284.96 for day & time) which you can then apply Excel's built in date formatting to, it will also work for charts and pivot table grouping.

Compare time stamps in Excel?

I have an issue with excel and would need an solution preferably without having to use VBA code.
My excel document has two sheets:
Rawdata sheet: contains data; column A has content in timestamp format (dd-mm-yyyy hh:mm:ss)
Calculations sheet: uses countif() and sumif() formulas
Now I have a set of date parameters in my calculation sheet, also formatted with the timestamp format as seen above. I now want to use those parameters in my formulas as conditions, for example:
countif(Rawdata!A:A;">=Parameter1";Rawdata!A:A;"<Parameter2")
Although I have data that fits those parameters only 0 is displayed.
Is there any problem with the syntax of the formular itself or is the issue caused by errors concerning the formatting of the date parameters/ data??
if Parameter1 and Parameter2 are named ranges you need to separate the content form the literal String
countif(Rawdata!A:A;">=" & Parameter1;Rawdata!A:A;"<" & Parameter2)

Resources