There are many, many questions and quality answers on SO regarding how to prevent leading zeroes from getting stripped when importing to or exporting from Excel. However, I already have a spreadsheet that has values in it that were truncated as numbers when, in fact, they should have been handled as strings. I need to clean up the data and add the leading zeros back in.
There is a field that should be four characters with lead zeros padding out the string to four characters. However:
"23" should be "0023",
"245" should be "0245", and
"3829" should remain "3829"
Question: Is there an Excel formula to pad these 0's back onto these values so that they are all four characters?
Note: this is similar to the age old Zip Code problem where New England-area zip codes get their leading zero dropped and you have to add them back in.
=TEXT(A1,"0000")
However the TEXT function is able to do other fancy stuff like date formating, aswell.
The more efficient (less obtrusive) way of doing this is through custom formatting.
Highlight the column/array you want to style.
Click ctrl + 1 or Format -> Format Cells.
In the Number tab, choose Custom.
Set the Custom formatting to 000#. (zero zero zero #)
Note that this does not actually change the value of the cell. It only displays the leading zeroes in the worksheet.
I hit this page trying to pad hexadecimal values when I realized that DEC2HEX() provides that very feature for free.
You just need to add a second parameter. For example, tying to turn 12 into 0C
DEC2HEX(12,2) => 0C
DEC2HEX(12,4) => 000C
... and so on
I know this was answered a while ago but just chiming with a simple solution here that I am surprised wasn't mentioned.
=RIGHT("0000" & A1, 4)
Whenever I need to pad I use something like the above. Personally I find it the simplest solution and easier to read.
I am not sure if this is new in Excel 2013, but if you right-click on the column and say "Special" there is actually a pre-defined option for ZIP Code and ZIP Code + 4. Magic.
If you use custom formatting and need to concatenate those values elsewhere, you can copy them and Paste Special --> Values elsewhere in the sheet (or on a different sheet), then concatenate those values.
Even this will work nicely
REPT(0,2-LEN(F2)&F2
where 2 is total number of digits, for 0 ~ 9 -> it will display 00 to 09 rest nothing will be added.
Assuming that the number you want to pad is in cell A1, and the "padding number of zeros" is 4 ,
e.g.
"23" should be "0023",
"245" should be "0245", and
"3829" should remain "3829"
then
=TEXT(A1,REPT("0",4))
Related
In my job I have to enter warranty information so that POs can be cut. Vendors are very particular with how this is entered and each one has their own format.
One of them requires data be entered:
SN:
MACHTYPE:
Further, the information for this is sent in a single composite number, something like:
10Y754235FUYJ9
Requiring the final input of data to be:
SN:10Y75423
MACHTYPE:UYJ9
The first 4 digits of the composite are the MACHTYPE and the final 8 are the Serial Number.
The impasse I've reached is I can't seem to get auto-fill to replicate the skipping of lines as I've formatted. It will jump a number of lines equal to however many I've selected.
Any ideas about getting it to replicate the first four Formatted Data? I've been throwing myself at this for a couple hours now.
Thanks in advance!
I think you're over complicating it by using two rows for the Formatted Data. I'd wrap the text (Home tab > Alignment section) for the cells in column F and use the following formula to insert a new line:
=CONCATENATE("SN:",B1,CHAR(10),"MATCHTYPE:",C1)
Then just leave the formatting like you had it by row...
Okay, let's focus on the title. When A1 is 10Y754235FUYJ9,
="SN:"&LEFT(A1,8)&CHAR(10)&"MATCHTYPE:"&RIGHT(A1,4)
will give you the output:
SN:10Y75423
MATCHTYPE:UYJ9
And don't forget to set the wrap text checked at Format Cells menu.
On a daily basis I need to load data to one of our systems. However Excel deletes the previous zeros in front of the contractor IDs. So i have to add THREE zeros manually. I normally use the CONCATENATE function however now the IDs are coming differently so some IDs now only need to have TWO zeros added.
example:
ID
911111
I use concatenate to make it look like:
000911111
I came up with the IF formula that detects if the ID starts with a number NINE, to concatenate TWO zeros and if not, then to add THREE zeros.
example:
=IF(LEFT(A32,1)="9",CONCATENATE("00",A32),CONCATENATE("000",A32))
Now I want to create this formula as a custom defined so I do not have to write down the formula ever time I work on the data every day.
Any suggestions I will really appreciate.
In addition to the formatting responses provided in the comments, you could use the RIGHT function to cut off the leading zeroes to the appropriate amount.
For example, assuming A1 holds a string of numbers, between 0 & 9 digits long. We can create text representing a 9 digit string, with as many leading zeroes as necessary, as follows:
=RIGHT(REPT("0",9) & A1,9)
REPT("0",9) tells Excel to repeat the character "0" 9 times. It then tacks on whatever text is in A1. Then it takes only the rightmost 9 characters of the concatenation.
I generally would recommend the Formatting options noted in those comments, unless you need the text to be 9 characters for other formula purposes.
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.
I have something like this:
A1: ABC-DE442
B1: 0069-1234-12
I'm trying to make a formula that will look at A1, get the number 442, and then check if B1 is in the format of 4 numbers, a dash, 4 numbers, a dash, and then finally 2 numbers. I want to make sure that this could work with any number besides 442 though. So if I had like 123 instead of 442, it would need to be 1 number, dash, 2 numbers, dash, and three numbers.
It would also need to give me an error if I had something like this:
A1: ABC-DE442
B1: 004-2345-34
because it only has three numbers in the first section before the dash.
Hopefully I have been clear enough in what I'm trying to do. Any help would be greatly appreciated.
So in the end, this is the giant formula I had to create:
=IF(LEN(LEFT(J4,FIND("-",J4)-1))=VALUE(MID(I4,9,1)),IF(LEN(LEFT(MID(J4,FIND("-",J4)+1,999),FIND("-",MID(J4,FIND("-",J4)+1,999))-1))=VALUE(MID(I4,10,1)),IF(LEN(MID(MID(J4,FIND("-",J4)+1,999),FIND("-",MID(J4,FIND("-",J4)+1,999))+1,999))=VALUE(MID(I4,11,1)),"True","Doesn't match code part 3"),"Doesn't match code part 2"),"Doesn't match code part 1")
J15: 0069=LEFT(J5,FIND("-",J5)-1)
J16: 4=VALUE(MID(I5,7,1))
J17: 4=LEN(J15)
J18: 0469-56=MID(J5,FIND("-",J5)+1,999)
J19: 0469=LEFT(J18,FIND("-",J18)-1)
J20: 4=VALUE(MID(I5,8,1))
J21: 4=LEN(J19)
J22: 56=MID(J18,FIND("-",J18)+1,999)
J23: 2=VALUE(MID(I5,9,1))
J24: 2=LEN(J22)
Hopefully all of these formulas will make it easier to understand how I combined everything together.
I essentially put every formula into one giant one using the information used in the answer I said was right. For the first part, I took the LEN(D1) but had it all written out, and then set it equal to the =VALUE(MID(A1,7,1)).
Then, for the true part of that if statement, I had to first get A1 without D1, then get it to just be the middle section using the LEFT formula, and then finally taking the LEN of that. Checked to see if it was equal to the =VALUE(MID(I4,10,1)), which was the next number in B1.
If that was true, I went to the final IF statement which again had to go through the same process of getting the last section of A1 on it's own, taking the length of that, and seeing if it was equal to =VALUE(MID(I4,11,1)).
Finally, I just created different print statements for the last true, and every false section of the IF statements.
Moral of the story, I was able to make a formula that works with every single number combination because it turns out B1 could only be in the form of AB - CDE###, so I could always find that number in B1 and compare it.
This should be possible with Excel formulae but it will get a bit complicated. The functions you're probably going to want to make use of are FIND, LEFT, LEN, MID, and VALUE.
For example,
C1: =FIND("-",B1)
will return the position of the first dash in the string to be tested, which for your example is 5. Then
D1: =LEFT(B1,C1-1)
would return the characters before that dash, i.e. '0069'. Meanwhile you need to get the lengths of the three runs of digits out of your first string (the one in A1). Will that string always be the same length? If so you can use e.g.
=VALUE(MID(A1,7,1))
which returns the 7th character of the string in A1 and converts it to a number (4). Now you can test whether that value is equal to the length of the first run of characters, i.e. LEN(D1).
But are those characters all numeric? Well, you can try converting them to a number with VALUE(D1) which will return #VALUE if the argument can't be made into a number.
OK, that checked the first run of characters, what about the rest of them? Well,
=MID(B1,FIND("-",B1)+1,999)
will give you the remainder of the original string after the first dash, which you can analyse in the same way, and so on.
It's easiest to develop these formulae by using lots of cells to hold the intermediate values as you work them out, but once you're confident these are working OK you can consolidate them into fewer cells. Whatever you come up with, make sure you test it with lots of different inputs to check that your formulae respond to them in the way that you expect. (In particular, check what happens if you have one or more spaces after a dash, as VALUE will ignore leading spaces - you might want to use a SUBSTITUTE function on your initial text to remove any spaces.)
Seems like it would be a simple thing really (and it may be), but I'm trying to take the string data of a column and then through a calculated column, replace all the spaces with %20's so that the HTML link in the workflow produced email will actually not break off at the first space.
For example, we have this in our source column:
file:///Z:/data/This is our report.rpt
And would like to end up with this in the calculated column:
file:///Z:/data/This%20is%20our%20report.rpt
Already used the REPLACE, and made up a ghastly super nested REPLACE/SEARCH version, but the problem there is that you have to nest for EACH potential space, and if you don't know how many up front, it doesn't work, or will miss some.
Have any of you come across this scenario and how did you handle it?
Thanks in advance!
As far as I know there is no generic solution using the calculated-column syntax. The standard solution for this situation is using an ItemAdded (/ItemUpdated) event and initializing the field value from code.
I was able to solve this issue for my circumstances by using a series of calculated columns.
In the first calculated column (C1) I entered a formula to remove the first space, something like this:
=IF(ISNUMBER(FIND(" ",[Title])),REPLACE([Title],FIND(" ",[Title]),1,"%20"),[Title])
In the second Calculated column (C2) I used:
=IF(ISNUMBER(FIND(" ",[C1])),REPLACE([C1],FIND(" ",[C1]),1,"%20"),[C1]).
In my case, I wanted to encode upto four spaces, so I used 3 calculated columns (C1, C2, C3) in the same fashion and got the desired result.
This is not as efficient as using a single calculated column, but if SUBSTITUTE will not work in your SharePoint environment, and you cannot use an event handler or workflow, it may offer a workable alternative.
I actually used a slightly different formula, but it was on a work machine to which I don't have access at the moment, so I just grabbed this formula from a similar S.O. question. Any formula that will replace the first occurrence of a space with "%20" will work, the trick is to a) make sure the formula returns the original string unchanged if it does not have more spaces in it, and b) test, test, test. Create a view of your list that has the field you are trying to encode, plus the calculated fields, and see if you are getting the results you want.
so that the HTML link in the workflow produced email will actually not break off at the first space.
The browser only does this if you have not enclosed your link in quotes
If you wrap the link in quotes, it does not cut off at the first space
In a SharePoint Formula it would be:
="""file:///Z:/data/This is our report.rpt"""
becuase two quotes are the SP escape notation to output a quote
You can use this formula (Start trim for 1, in my case was 4):
=IF(ISBLANK([EUR Amount]),"",(TRIM(MID([EUR Amount],4,2))&TRIM(MID([EUR Amount],6,2))&TRIM(MID([EUR Amount],8,2))&TRIM(MID([EUR Amount],10,2))&TRIM(MID([EUR Amount],12,2))&TRIM(MID([EUR Amount],14,2)))*1)