Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

leading zeros

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB
I am pulling a value from a row:

safeID = (lookUpRow.Cells(1, 2))

An example number is 001234

I then want to place this value in another cell via

Sheets(targetWorkSheetName).Cells(targetRow, 7).Value = safeID

However the leading zeros are lost. How do I prevent this?


 
prob the format of the cell you are putting it into isnt a string? i think it is something like @ format? record a macro and see what it spits out, if not then make sure the format of your cell is @ text before hand?
 
1stly - your code is double using your row counter - the CELLS method takes a row argument so instead of:
Code:
safeID = (lookUpRow.Cells(1, 2))
[COLOR=purple]you can write[/color]
safeID = Cells(lookUpRow, 2)
2ndly, there are several ways of doing this - if the numbers are all 6 digits with leading zeroes then:
Code:
Sheets(targetWorkSheetName).Cells(targetRow, 7).Value = FORMAT(safeID,"000000")
However, this is unnecessary - there is no need to use a variable as you are simply manipulating data (unless it is to be used again later) so you can change your code to
Code:
Sheets(targetWorkSheetName).Cells(targetRow, 7).Value = format(Cells(lookUpRow, 2).value,"000000")

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top