Trim function causes date format change - vba

Firstly, important to note that I'm in the UK so standard date format is dd/mm/yyyy
In a A1, I have a date: 02/05/2017 (dd/mm/yyyy)
I can confirm this in the immediate window:
?CLng(Range("A1").Value)
42857
Now, if I do the following:
Range("A1").Value = Range("A1").Value
you can probably guess, nothing happens - the date is still 02/05/2017 and the numeric value is still 42857
But if I use trim with it:
Range("A1").Value = Trim(Range("A1").Value)
The date is changed to 05/02/2017. This isn't just formatting - the numeric value has also changed to 42771.
What is it about the Trim() method that causes the date to be read in US format and then converted back to UK format with a new date value? Is this a bug?

From the discussion in comments:
The default or "token" format in VBA (not Excel itself, as Macro Man rightly pointed out) is US English - regardless of regional settings or cell formatting.
When you do VBA text functions on a date, the output of those functions are in text format. So the result of Trim(Range("A1").Value) is a string. This string happens to resemble a proper US date, so when you insert it into a cell, Excel recognizes it as a US date.
So two implicit conversions happen. The first happens when you read the cell contents and pass it to trim(): date->text conversion; the second happens when you write it back to an Excel cell: text->date conversion. The second conversion has no information about the format, so it assumes US English.
(You should be able to achieve the same result with any text function, not just trim().)

I found out that if you add "'" before the date, the date is not altered.
Range("A1").Value = "'"&Trim(Range("A1").Value)

Related

vba customize cells format

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.
Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.
Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

What is the numberformat code for Short date?

I know that I can write the following to format a number as a date with the appearance yyyy-mm-dd:
Range("A1").NumberFormat = "yyyy-mm-dd"
I know that I can also write:
Range("A1").NumberFormat = "m/d/yyyy"
and all kinds of things.
But is there a way I can set the number format to become a general short date and leave it up to the user's system to decide the exact appearance?
I'm looking for an expression like:
Range("A1").NumberFormat = "Short Date" '(<-This does not work)
It is a bit tricky. If you have a look at a cell's properties, format tab, category Date, you will see some formats beginning with an asterisk (*), those respond to the changes of the user locale. Try setting your cell to the first element, short date, then get its numberformat value in the immediate window with ?activecell.NumberFormat. You'll see that it is "m/d/yyyy", so your snippet does the job well. Just try changing your locale setting in control panel, the format of the cell will change accordingly.
Please try this
Format(Range("A1"), "Short Date")
It will return the system short date value.

Date Formatting in Excel VBA

I have written a long procedure in VBA to manipulate a data set. Part of this involves using and formatting dates, and I can't get it to work properly.
The initial data as downloaded has dates in the format "yyyy-mm-ddThh:mm:ssZ" - e.g. 2014-12-11T04:59:00Z.
I then convert these into a date in UK format of "dd/mm/yyyy". Looping over all relevant cells, I use the following method:
initial_date = Range("A1").Value
Dim publish_date As Date
publish_date = DateValue(Mid(initial_date,9,2) & "/" & Mid(initial_date,6,2) & "/" & Mid(initial_date,1,4))
Range("A1").Value = publish_date
This seems to work fine. The cell automatically changes format to "Date" and when I calculate the difference between these dates and dates in another column, it works fine.
I then pick up these dates again and add to an array:
feed(1, 8) = Range("A1")
and then transfer this value into another array:
new_array(7, 1) = feed(1, 8)
Finally, I insert the value from the new array into a different cell. Having used UK date formatting thus far, I now want this value to display in the form "mm/dd/yyyy".
Range("E1") = new_array(7, 1)
Range("E1").NumberFormat = "mm/dd/yyyy"
This is where it goes wrong.
Running the code as above, the dates are all displayed as "dd/mm/yyyy" format. Excel changes the format of the cells to "custom". Finding the difference between these and other dates works, so the data seems to be stored correctly but not displaying as I want.
If I change the code to
Range("E1") = new_array(7, 1)
Range("E1").NumberFormat = "dd/mm/yyyy"
the dates still do not display correctly. Dates which can be written either way around (i.e. day is equal to or less than 12) display correctly, but are in fact the wrong date. i.e. 2014-12-11T04:59:00Z displays as 12/11/2014 which is what I want, but Excel thinks the date is the 12th November instead of 11th December. Dates such as 29/09/2014 which cannot be written both ways round display in UK format, but are not recognised properly by Excel which thinks the long date should be "29/09/2014" instead of "29 September 2014".
When I remove the formatting line completely, the results are the same.
I'm sorry for the rather long-winded explanataion, but there's clearly something I'm not understanding about how Excel and VBA handle, store and format dates. If anyone could enlighten me what's going wrong, I'd really appreciate it!
(Note, in all the code snippets above, where I quote e.g. Range("A1") this is shorthand. There is in fact a lot more code involved in looping and selecting values, but I know this works, so I am not concerned. The extracts above just demonstrate what happens for the first value in each loop.)
try
Range("E1").NumberFormat = "mm/dd/yyyy"
Range("E1") = Format(new_array(7, 1), "mm/dd/yyyy")

vba treat time as string

I am storing some values in my excel using vba and them comparing with other results. The results can be whatever: name, surname, address, time...
The problem that I have is that when I store a string that has a date format it takes it as time automatically. For example if I enter 8:15 it automatically convert it to 08:15 and then when I am comparing it with other value that is the same (8:15) it returns me false because it is not 08:15.
So basically the question is... how can I introduce a value like 8:15 without being converted to 08:15?
Two methods:
Put an apostraphe at the front of the value to tell Excel it's a string. Example: The value '8:15 will be interpreted as a string of value "8:15".
Use a formula to store the value as a string. Example: ="8:15" is also interpreted as a string of value "8:15".
I'm assuming this is when you're entering the values in Cells. the format of the cells is probably general, thus it formats things like numbers or dates different. Change it to text.
select all (or specific columns) right click - format cells - number tab - text option. it says there "text format cells are treated as text even when a number is in the cell. the cell is displayed exactly as entered"
edit
another option, in VBA use .FormulaR1C1 instead of .value when comparing. .value is the actual value of the cell (like a formula result) where as .formulaR1C1 will give you the actual entered text. so .formulaR1C1 might get you the actual entered 8:15 instead of the corrected value: 08:15.

Date format issue in SSRS

I have an issue with date format in my SSRS. I am saving date from DateTimePicker to database. From there I am taking display in my datagridview using following
dgv.items(0,2).value=Format(Cdate(dsSaver.tblInv.rows(0).items(0)),"dd-MMM-yyyy")
This displays it correctly (04-Nov-2011) but when I take date from the same database to my SSRS using
="Dated: " &Format(cdate(Fields!InvDate.Value),"dd-MMM-yyyy")
It displays it like 11-Apr-2011.
I have tested all winforms fare displaying it right but all SSRS are displaying it wrong.
Please advise.
A couple of things are going on here. The date is being saved appropriately but is being displayed incorrectly due to your formatting options. This line is quite problematic:
="Dated: " & Format(cdate(Fields!InvDate.Value), "dd-MMM-yyyy")
CDate takes a value, generally a string, and converts it to a date, which you are then taking and formatting back into a string. Now, by default reports are set to have their Language property set to English (United States) so the CDate function is taking the string representation of the date 04-Nov-2011 to be 04/11/2011 which it is then converting, using the US format of MM-dd-yyyy (not the Pakistani one) into being the date 11-Apr-2011 because it thinks the month comes first.
So, you should change your Language setting of your report to =User!Language so that it supports whatever the user's language is and will format things appropriately. This may be enough to make your expression work.
Regardless, if Fields!InvDate.Value is being supplied as a date field (as it should be) there is no need for the CDate function and this should work:
="Dated: " & Format(Fields!InvDate.Value, "dd-MMM-yyyy")
There is also the FormatDateTime function but unfortunately it doesn't support the format you want to use.
Have you looked at the RDLC options for Formatting a Report: Format the Date?