简体   繁体   中英

Convert to text truncating leading zeros

I am using =TEXT() function in Excel to convert alphanumeric to a string. All of them convert except part numbers that have a leading zero, eg 012345.

Currently my function is =TEXT(Input!B10, "0") . I have tried different variations of this without success. I have searched the Interwebs for an answer, also without success. Everything converts to a string EXCEPT for numbers with ONE leading zero. I have even setup a new workbook to test, and I get the same result. This is part of a larger macro to get custom prices for my ERP system.

Simplistically you can just do this. This would assume you have 0123456 in B10 formatted as Text. It will format to keep the same amount of leading zeros as in the original.

=TEXT(B10,REPT(0,LEN(B10)))

The TYPE Worksheet Function

=IF(TYPE(INPUT!B10)=2,B10,TEXT(INPUT!B10,"#"))

If you don't want to show other text values use this:

=IF(TYPE(B10)=2,IF(LEFT(B10,1)="0",B10,""),TEXT(B10,"#"))

If you want to convert empty cells to 0 then use:

=IF(TYPE(INPUT!B10)=2,B10,TEXT(INPUT!B10,0))

VBA Help

Type

Returns the type of value. Use TYPE when the behavior of another function depends on the type of value in a particular cell.

Syntax TYPE(value)

'Value' can be any Microsoft Excel value, such as a number, text, logical value, and so on.

If value is   TYPE returns 
Number             1 
Text               2 
Logical value      4 
Error value       16 
Array             64 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM