Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i am trying to read a string from a particular cell and then split the string from Zero on-wards and print the value in another cell

Issue-> In the local window when i am running the code it is showing the correct value "0002268 " but when i insert the same value in another cell its showing "2268".Please let me know what can be possible reason.

Sub newid()

Dim FirstZero As Long
Dim Name As Variant

Sheet2.Activate

Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Value = "Orgnization ID"

Numberofrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To Numberofrow

Y = Range("A1").Offset(i, 0).Value
Lengthofstring = Len(Y)

FirstZero = InStr(1, Y, 0)
Debug.Print FirstZero

Name = Right(Y, Lengthofstring - (FirstZero - 1))

Range("A1").Offset(i, 0).Offset(0, 4).Value = Name

Next i

End Sub


What I have tried:

Sub newid()

Dim FirstZero As Long
Dim Name As Variant

Sheet2.Activate

Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E1").Value = "Orgnization ID"

Numberofrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To Numberofrow

Y = Range("A1").Offset(i, 0).Value
Lengthofstring = Len(Y)

FirstZero = InStr(1, Y, 0)
Debug.Print FirstZero

Name = Right(Y, Lengthofstring - (FirstZero - 1))

Range("A1").Offset(i, 0).Offset(0, 4).Value = Name

Next i

End Sub
Posted
Updated 19-Feb-18 9:17am

1 solution

Because it's a number - and unless the column is formatted as Text, or the number is preceded by a quote, Excel will treat it as a number. And numbers do not have leading zeros...
Prefix the first 0 with a quote and Excel should treat it as text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900