0

I want to remove all the leading 0's from the numbers in this column, without affecting the rows that start with a letter.

enter image description here

Community
  • 1
  • 1
  • 4
    I think you may have forgotten to include in your question what you have tried and what aspect of it isn't working. – Ambie Jul 27 '16 at 15:06

5 Answers5

4

For a non-VBA solution, you can highlight the entire row and go to Data>Text-To-Columns and then hit "Finish". Numbers will be converted to numeric format and strings will remain untouched.

For a VBA solution, you could iterate through the cells:

 For each rngCell in Sheet1.Columns("A").Cells

 Next rngCell

And within that iteration, use isnumeric() to test before replacing

 If isnumeric(rngCell.value) Then rngCell.value = rngCell.value + 0

Or something along those lines. You could also record that text-to-columns bit and just sub out your column's range in the VBA. Depending on how much control you need over your data transformation.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • 1
    ... and of course the number format shouldn't be something like this: `rngCell.NumberFormat = "0000000000"` ;) – Ralph Jul 27 '16 at 15:20
4

Another Non VBA Solution... (Simplest of all)

  1. Highlight the data
  2. You will see an Exclamation Mark next to your data as shown in screenshot. Click on it
  3. Click on Convert To number and you are done :)

enter image description here

Result

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
3

you can use this formula. my formula source data starts from B3.

=REPLACE(B3,1,FIND(LEFT(SUBSTITUTE(B3,0,"")),B3)-1,"")

enter image description here

Rosetta
  • 2,665
  • 1
  • 13
  • 29
1

Try to convert the cell to a number and then back to string - if it fails it already was a string. vba convert string to int if string is a number

Edit:

Try to solve it with an regex:

With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = "^0*"
End With

If regEx.Test(strInput) Then
    strInput = regEx.Replace(strInput, "")
End If

Source: How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

0

You could do it with a formular with or without VBA:

=IF(ISNUMBER(Value(A1));Value(A1);A1)
thentt
  • 16
  • 4
  • Yes, of course i have tested it, works perfectly. Didn't it work for you? – thentt Jul 28 '16 at 10:45
  • I am working on Excel 2016. As per my understanding The ISNUMERIC function can only be used in VBA code in Microsoft Excel whereas you have mentioned that one can do it with a formula with or without VBA. Another Worksheetfunction ISNUMBER works fine for me in the formula. – skkakkar Jul 28 '16 at 13:33
  • Yeah, you are right. Thank you. I had to test it in an excel with an different language package. Lost in Translation. – thentt Aug 02 '16 at 14:38