Store Number as Text

jfish1288

Board Regular
Joined
Jun 22, 2011
Messages
116
I'm using an excel file to generate a list of UPC codes to send to a printer.
The UPC code needs to be 12 digits.
In the source excel file the UPC codes are stored as text with a leading zero.
Using the code below, they are populating the new list as numbers without the leading zero.
Formatting as a 12 digit number does not work because the printer does not read the leading zero.
It needs to be stored as text.
What am I doing wrong?

Source sheet examples (number is stored as text with the little green corner in the cell and the warning sign)
ColumnAColumnBColumnCColumnDColumnEUPC
A2B2C2D3E4097963547574
A3B3C3D3E3097963664127

<tbody>
</tbody>

Current Output
ColumnAColumnBUPC
A2B297963547574
A3B397963664127

<tbody>
</tbody>


Code:
Dim UPC_code As String

UPC_code = sourceWS.Cells(iRow, 6).Text
expandWS.Cells(printRow, 3).Value = UPC_code
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The ".Value" is coercing your text value back to numeric. Get rid of it here:
Code:
expandWS.Cells(printRow, 3)[COLOR=#ff0000].Value[/COLOR] = UPC_code
like this:
Code:
expandWS.Cells(printRow, 3) = UPC_code
 
Upvote 0
If the cell is formatted as number or general, it will interpret a number as a number, whether that number is stored in VBA variables as a string is irrelevant. I would suggest trying to format the cell/range as text fist:

Code:
Dim UPC_code As String


UPC_code = sourceWS.Cells(iRow, 6).Text
expandWS.Cells(printRow, 3).NumberFormat = "@"
expandWS.Cells(printRow, 3).Value = UPC_code
 
Upvote 0
If the cell is formatted as number or general, it will interpret a number as a number, whether that number is stored in VBA variables as a string is irrelevant. I would suggest trying to format the cell/range as text fist:

Code:
Dim UPC_code As String


UPC_code = sourceWS.Cells(iRow, 6).Text
expandWS.Cells(printRow, 3).NumberFormat = "@"
expandWS.Cells(printRow, 3).Value = UPC_code


That did it. Thanks!
 
Upvote 0
I still have the same issue
Odd. It worked for me when I tested it out.
My next suggestion was going to be to pre-format the cell/column/row as Text, but looks like Modesto73 already got you there.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top