Label = whole number - How do I set this?

  • Thread starter Thread starter Bafa
  • Start date Start date
B

Bafa

Private Sub UserForm_Initialize()
Label2.Caption = Worksheets("Data").Range("j20").Value
End Sub

I am using this to draw calculated data from my worksheet and past
into my UserForm. I set my work sheet cell J20 up to use whole number
only, but when my UserForm calls the data up it is still being tol
decimal values. Instead of 125.5 showing up in my user form I want t
see just 125 How can I do this please?

Also any suggested reading web sites for someone just wetting thei
feet in this
 
Not sure if this is the best way to do it but here is a way to do it:

Private Sub UserForm_Initialize()
Dim num As Integer
num = Worksheets("Data").Range("J20").Value
Label2.Caption = num
End Sub

Have a go and see how it goes
 
On your spreadsheet you have probably formatted the cell to show whole
numbers, but that doesn't actually change the cell value.

You can format in VBA as well before you display the value. Try:
Label2.Caption = Format(Worksheets("Data").Range("j20").Value,"#")

Note: This is also a format and will not change the actual value.

Hope this helps,
Dan
 
Bafa,
If you want what appears in the cell, use the .Text property.
If you want the actual value stored in the cell, use the .Value property
Label2.Caption = Worksheets("Data").Range("j20").Text

Or you can format the .Value how you wish
Label2.Caption = Format(Worksheets("Data").Range("j20").Value,"0"

NickHK
 
Format works, but 125,5 is changed to 126.
That's the same as Round( Worksheets("Data").Range("j20").Value, 0 )
He asked for 125.


Label2.Caption = Left(LTrim(Str(Worksheets("Data").Range("j20").Value)),
InStr(1, LTrim(Str(Worksheets("Data").Range("j20").Value)), ".",
vbTextCompare) - 1)

will give 125.
 
I guess I just assumed that the 126 was a typo. If only integer portion of
the number is desired, the INT function would probably be a little easier:

Label2.Caption = Int(Worksheets("Data").Range("j20").Value)

That being said, I think Nick HK's suggestion to use ".text" instead of
".value" is the best solution.

Dan
 
I love how active this forum is. I am learning a lot. The .tex
solution should work just fine for my needs. Thanks to all wh
answered
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top