How to prevent Excel from handling strings containing a colon as formulas - excel

I am generating csv files, and some cells have the format nn:nnnn , i.e. digits separated by a colon. It's not a time format nor a date format, it's just text cells and I don't want them to be re-formatted at all.
I've added some logic to my code in order to identify if it looks like a legal time format or a date, and if so, I wrap that string like this ="nn:nnnn". But I'm not interested in adding those characters to all the cells.
It almost solved my problem, but there are still some cases such as 07:1155 that MS Excel insists to translate it as 1.09375. Other cells such as 68:0062remain intact. Is there a way to recognize what strings are going to be calculated or translated?
Is there any workaround such as any set-up in MS Excel to tell it not to perform any translation on these kind of text?

Instead of just opening your CSV in Excel, you might try doing (Menus) Data/Get External Data/From Text. Or if you're using VBA, that would be something like:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;H:\csvtest.csv" _
, Destination:=Range("$A$3"))
.Name = "csvtest_1"
.FieldNames = True
etc
End With
You may need to specify Text as the incoming column format.

I've got the following answer from Mr. JP Ronse at the Microsoft Community forum
Try to precede a string like 07:1155 with a single quote.
A single quote prevents Excel from interpreting the value.
For some reason Excel interpret a string like 07:1155 as a time and translates it to the value.
Excel sees 07:1155 as 7 hours and 1155 minutes, translated to values:
07:00 => 0.291666666666667
1155 minutes => (1155/60)/24 => 0.802083333333333
The sum is 1.09375
It looks as there is no translation on values like n:00nn or like n:0nnnnn
Checking on the 2 n's after the colon (not 00) could be a workaround.

Related

Text to columns: How to prevent date formatting of decimal numers?

With a dataset such as this:
Date, Value
04.03.2020, 13.35
04.03.2020, 13.8
04.03.2020, 21.21
You can split the data using Data > Text to columns and specifying the value separator.
The problem is that Excel recognizes 13.8 as august 13, so the output is:
Date Value
04.03.2020 13.35
04.03.2020 13.aug
04.03.2020 21.21
How can I make sure that Excel never interprets decimal numbers such as 13.8 as dates?
I'm working in a region that uses , as decimal separator, but I often have to work with data that is set up with . as the decimal separator. One work-around is of course to replace , with ; and . with , before opening a .csv file. And if the only other solution is to set it up with VBA, I'm perfectly able to do so myself. But I often find myself trying to help colleagues on their computers without my own VBA configurations ready to go. So if there's any other way to do this using standard Excel system settings, that would be great!
This little problem has bugged me for years and I'm eager to get rid of it once and for all.
Edit:
I'm running Excel version 1908 on Windows 7, Office 365.
This problem often occurs when I'd like to inspect csv files that are recognized as Excel files on my system. There have been suggestions to format the cells as General before splitting the data. That does not seem to work on my end.
Another solution is to get what you want with formulas:
Formula for Dates:
=TEXT(LEFT(A2,FIND(",",A2,1)-1),"General")
Formula for Values:
=TEXT(MID(A2,(FIND(",",A2,1)+2),(LEN(A2)-(FIND(",",A2,1)+1))),"General")
Results:
During Text to Column process at Step 3 you have the option to to select Data Column Format you could also select the Value column and play around with Text or General formating.

Locale-independent Text function in Excel

I need to format dates in excel, and I'm trying to use the TEXT formula. The problem is that Excel's intepretation of the arguments changes when the locale changes.
For example: if I have a date in cell A1, that i'd like to convert to text, in the year-month-day-format, I have to use =TEXT(A1, "yyyy-mm-dd") if my PC has an English-language locale, but =TEXT(A1, "jjjj-MM-tt") (I kid you not, the M has to be upper case) if it has a German-language locale. This makes the document unportable. (The second argument is plain text and therefore not converted when changing locale.)
Remarks:
This is just an example, I know I could do the long =YEAR(A1) & "-" & TEXT(MONTH(A1), "00") & "-" & TEXT(DAY(A1), "00") in this case. I'm wondering about the more general case.
The date should not just be displayed in a certain format, it should actually be a string. For someone viewing the file this doesn't make a difference, but when using it in other formulas, it does.
I could write a UDF in VBA to solve the issue, but I cannot use VBA in this document.
I do not care about changing the names of the months etc. It's fine, if the name of the month is June or Juni depending on the locale.
I want to stress that the issue occurs due to the PC's locale - not due to the GUI language of the MS Office version. In the example above, Excel's GUI and formulas were in English in both examples; I just changed the locale on the machine.
Many thanks
Here is a slightly cheaty method: Use a VLOOKUP on a value that will change based on your System Language - for example TEXT(1,"MMMM")
=VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE)
In English: Text(1,"MMMM") = "January", so we do a VLOOKUP on the Array below to get "yyyy-MM-dd"
"January" , "yyyy-MM-dd" ;
"Januar" , "jjjj-MM-tt"
Auf Deutsche, Text(1,"MMMM") = "Januar", also wir machen einen SVERWEIS auf dem Array oben, um "jjjj-MM-tt" zu erhalten! :)
Then, just use that in your TEXT function:
=TEXT(A1, VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE))
Obviously, the main reason this works is that TEXT(1,"MMMM") is valid for both German and English. If you are using something like Filipino (where "Month" is "Buwan") then you might find some issues finding a mutually intelligible formatting input.
I found another possibility. It is not perfect in all cases (see below) but it also works with number formats to be locale independent. As I have the same issue with mixed language versions.
For this you make your own function in vba. Open the developer tools with Alt+F11 and create a new module file. Inside the module file paste something like this:
Function FormatString(inputData, formatingString As String) As String
FormatString = Format(inputData, formatingString)
End Function
Then you can use it in cell formulas with english formating strings. Like:
= FormatString(A1; "yyyy-mm-dd")
Advantage: It also works with number formats:
= FormatString(A1; "00.00")
In case (like Germany) your decimal separator is not a .
Drawbacks:
1 Not identical to TEXT function
this doesn't always work with date formatting as maybe expected and not exactly the same as the TEXT function:
FormatString(1; "MMMM")
does not return "January" but "December" because the 1 is taken as a date. Which is something like 31.12.1899.
2 Has to be saved with macros
You have to save the file as *.xlsm for this to work
Note (1): this answers only the case for locale-independent TEXT to format numbers with decimal symbols and digit grouping symbols. For date formatting, see Chronocidal's answer.
Note (2): this answer does not use VBA functions, which would require enabling macros. Enabling macros may not be possible depending on the company's security policy. If enabling macros is an option, Uwe Hafner's answer would be easier.
You can detect the decimal symbol and digit grouping symbol as follows. Enter the number 1 in a specific cell (e.g. A1) and the number 1000 in another cell (e.g. A2).
Decimal symbol: =IF(TEXT(INDIRECT("A1"),"0,00")="001",".",",")
Digit grouping symbol1: =IF(TEXT(INDIRECT("A2"),"#,###")="1000,",".",",")
This is assuming that the decimal symbol is either . or , and the digit grouping symbol is either , or . respectively. This will not detect unusual digit grouping symbols like (space) or ' (apostrophe).
With this information, you can set up a cell (or cells) with a formula that results in the format code you need to apply.
Suppose you need to format a number to two decimal digits and using the digit grouping symbol. You can assume that if the decimal symbol is . then the digit grouping symbol will be , and vice versa. You can do the following:
A1: 1
A2 (the formatting string): =IF(TEXT(INDIRECT("A1"),"0,00")="001","#,##0.00","#.##0,00")
A3 (contains an arbitrary number you wish to format)
A4 (the formatted number): =TEXT(A3,A2)
Technical note: the INDIRECT function is used intentionally because it is a volatile function. This guarantees that the formatting string and anything dependent on it is recalculated even if no data changed in the Excel document. If INDIRECT is not used, Excel caches results and will not recalculate the formatting string when the Excel document is opened on a PC with different locale settings.
1 - Also known as Thousands separator
The easy fix, whether directly custom formatting a cell or using TEXT(), is to use a country code for a language you know the proper formatting codes for.
For instance, I am in the US, have a US version of Excel, and am familiar with its date code formats. So I'd want to use them and to ensure they "come out" regardless of anyone's Windows or Excel version, or the country they are in, I'd do it like the following (for TEXT(), let's say, but it'd be the same idea in custom formatting):
=TEXT(A1,[$-en-US]"yyyy-mm-dd")
The function would collect the value in A1, ask Excel to treat it as a date, Excel would and would say fine, it's cool (i.e.: the value is, say, 43857 and not "horse") because it is a positive number which is a requirement for anything to be treated as a date, and let the function move on to rendering it as a date in the manner prescribed. Rather than giving an #ERROR! as it would for "horse" or -6.
The function would then read the formatting string and see the language code. It would then drop the usual set of formatting codes it loaded upon starting up and load in the formatting codes for English ("en") and in particular, US English ("US"). The rest of the string uses codes from that set so it would interpret them properly and send an appropriate string back to TEXT() for it to display in the cell (and pass on to other formulas if such exist).
I have no way to test the following, but I assume that if one were to use a format that displayed day of the week names or month names, they would be from the same language set. In other words, Excel would not think that even though you specified a country and language that you still wanted, say, Dutch or Congolese month names. So that kind of thing would still need addressed, but would be an easy fix too just involving, say, a simple lookup one could add though it'd be "fun" setting up the lookup table for each language one wanted to accomodate...
However, the basic issue that arises with this problem in general, is very, very easily solved with the country codes. They aren't even hard or arcane anymore now that the [$-409] syntax has been replaced with things like [$-en-us] and [$-he-IL] and so on.

Why do Excel values in parentheses become negative values?

A colleague and I encountered a behavior in Excel which isn't clear to us.
Background:
We have a tool which converts an Excel sheet into a table format. The tool calculates the formulas which are in excel and replaces variables inside it with specific values.
The excel tool is used by one of our customers who use values like (8) or (247).
These Value are automatically translated by excel to -8 or -247.
Question:
I saw that many people want to display negative numbers in parentheses. But why would Excel change values in parentheses to a negative number?
I know that I could simply change the cell config to text and this would solve the problem but I wonder if there is a reason for the behavior, since there seems to be no mathematical reason for this.
Its simply the different format of cells you are bringing the "values from" and "pasting to". ..... numbers with parentheses are in cells with "accounting" format and negatives are stored in general or standard number formated cells. To resolve you can change the format of destination cells to accounting using cell formatting as number>accounting.
To answer the why, it's because accountants put negative numbers in brackets for readability
Unfortunately, this is one of the excel feature/bugs that helps some folks and frustrates others. When opening a file or pasting content, excel will immediately and always try to parse any values into formats it deems appropriate, which can mess up data like:
Zip Codes / Tel. # → Numeric: 05401 → 5401
Fractions → Dates: 11/20 → Nov, 20th YYYY
Std. Errors → Negative Numbers: (0.1) → -0.1
For some workarounds , see Stop Excel from automatically converting certain text values to dates
Once the file is open/pasted, the damage is already done. At that point, your best bet is:
Updating the field and displaying as text (appending with ') to prevent re-casting
Formatting the field if the operation wasn't lossy and is just presenting the info differently
Running a clean if/else to pad or other convert your data based on the identified errors
Specific to displaying values back in parens, if excel is converting them and treating them like negative numbers (which may or may not be the appropriate way to actually store the data), you can apply a different format to positive and negative numbers to wrap back in parens.
It is standard practice to write negative values as numbers in parentheses, especially in accounting. This makes negative values stand out much more than a simple negative hyphen; compare -1 and (1).
Excel is a tool very commonly used by accountants and supports accountant-style spreadsheets. Therefore, entering (100) means having a value of -100, even if there is no minus hyphen!
Here is a fun fact, if you enter (-10), Excel will treat it as normal text.

Format multiple date entries as strings

I have an Excel file storing a thousand lines of dates. Each date seems to be (auto)formatted as a Date. A (PHP Excel) parser I'm using (really can't update/use another one) is parsing this to a string which will occur in the number of days till 1900.
Is there a way to format the values in Excel being simple text "08.03.1991" to get this file parsed correctly?
I could add a quote: "'08.03.1991" but I need an (Excel-based) one-action-solution for all the thousand lines.
Remark: Since this is a file of a user I can't just write simple VBA-Script or so to handle this since there will be new files in the future and the User needs to be able to solve this alone.
I admit I am not quite sure what you have and what you want but it may be worth trying: Select column of dates, apply Text to Columns with Tab as delimiter and in step 3 of 3 select Text.
You could use the TEXT function like this:
=TEXT(A1,"dd.mm.yyyy")
For more details have a look here

FIxing MS Excel date time format

A reporting service generates a csv file and certain columns (oddly enough) have mixed date/time format , some rows contain datetime expressed as m/d/y, others as d.m.y
When applying =TYPE() it will either return 1 or 2 (Excel will recognize either a text or a number (the Excel timestamp))
How can I convert any kind of wrong date-time format into a "normal" format that can be used and ensure some consistency of data?
I am thinking of 2 solutions at this moment :
i should somehow process the odd data with existing excel functions
i should ask the report to be generated correctly from the very beginning and avoid this hassle in the first place
Thanks
Certainly your second option is the way to go in the medium-to-long term. But if you need a solution now, and if you have access to a text editor that supports Perl-compatible regular expressions (like Notepad++, UltraEdit, EditPad Pro etc.), you can use the following regex:
(^|,)([0-9]+)/([0-9]+)/([0-9]+)(?=,|$)
to search for all dates in the format m/d/y, surrounded by commas (or at the start/end of the line).
Replace that with
\1\3.\2.\4
and you'll get the dates in the format d.m.y.
If you can't get the data changed then you may have to resort to another column that translates the dates: (assumes date you want to change is in A1)
=IF(ISERR(DATEVALUE(A1)),DATE(VALUE(RIGHT(A1,LEN(A1)-FIND(".",A1,4))),VALUE(MID(A1,FIND(".",A1)+1,2)),VALUE(LEFT(A1,FIND(".",A1)-1))),DATEVALUE(A1))
it tests to see if it can read the text as a date, if it fails, then it will chop up the string, and convert it to a date, else it will attempt to read the date directly. Either way, it should convert it to a date you can use

Resources