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)
[TABLE="width: 500"]
<tbody>[TR]
[TD]ColumnA[/TD]
[TD]ColumnB[/TD]
[TD]ColumnC[/TD]
[TD]ColumnD[/TD]
[TD]ColumnE[/TD]
[TD]UPC[/TD]
[/TR]
[TR]
[TD]A2[/TD]
[TD]B2[/TD]
[TD]C2[/TD]
[TD]D3[/TD]
[TD]E4[/TD]
[TD]097963547574[/TD]
[/TR]
[TR]
[TD]A3[/TD]
[TD]B3[/TD]
[TD]C3[/TD]
[TD]D3[/TD]
[TD]E3[/TD]
[TD]097963664127[/TD]
[/TR]
</tbody>[/TABLE]

Current Output
[TABLE="width: 500"]
<tbody>[TR]
[TD]ColumnA[/TD]
[TD]ColumnB[/TD]
[TD]UPC[/TD]
[/TR]
[TR]
[TD]A2[/TD]
[TD]B2[/TD]
[TD]97963547574[/TD]
[/TR]
[TR]
[TD]A3[/TD]
[TD]B3[/TD]
[TD]97963664127[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Dim UPC_code As String

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

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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