ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   unexpected result from inputbox (https://www.excelbanter.com/excel-programming/350548-unexpected-result-inputbox.html)

HRman

unexpected result from inputbox
 
Hi,

I have a piece of code that promts for an employee number which may or may
not have a leading zero. If I print out the employee number using msgbox it
has the leading zero, but when i place it on a worksheet, the leading zero
disappears.

dim empno as string

empno = inputbox("enter the employee number")
msgbox ""+empno+"<"
worksheets("sheet1").cells(1,2).value = empno


Any suggestions ?

Toppers

unexpected result from inputbox
 
Hi,
Cell must be defined as Text not General - try the following :

Dim empno As String

empno = InputBox("enter the employee number")

Worksheets("sheet1").Cells(1, 2).NumberFormat = "@"
Worksheets("sheet1").Cells(1, 2).Value = empno

"HRman" wrote:

Hi,

I have a piece of code that promts for an employee number which may or may
not have a leading zero. If I print out the employee number using msgbox it
has the leading zero, but when i place it on a worksheet, the leading zero
disappears.

dim empno as string

empno = inputbox("enter the employee number")
msgbox ""+empno+"<"
worksheets("sheet1").cells(1,2).value = empno


Any suggestions ?


Kris

unexpected result from inputbox
 
HRman wrote:
Hi,

I have a piece of code that promts for an employee number which may or may
not have a leading zero. If I print out the employee number using msgbox it
has the leading zero, but when i place it on a worksheet, the leading zero
disappears.

dim empno as string

empno = inputbox("enter the employee number")
msgbox ""+empno+"<"
worksheets("sheet1").cells(1,2).value = empno


Any suggestions ?


Because Execel treats your result as a number and cuts leading zeros.

worksheets("sheet1").cells(1,2).value = "'" & empno




All times are GMT +1. The time now is 10:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com