label.caption = 1

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I just watched a video. He created a form then inserted a label and a command button. Then he made the label Caption =1, then clicked on command button and wrote the code below.
So when a user click the button the caption will increment by 1
Why is that? I changed the caption from 1 to Hello and the code stopped working. I thought the caption is a text, even when I type 1 it should be considered as a text, no? Thank you so much.

Code:
Private Sub myadd_Click()
myform.mycount.Caption = myform.mycount.Caption + 1
End Sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Yes, it's alway text, so must transform in integer/double before, like:
Code:
myform.mycount.Caption = Cint(myform.mycount.Caption) + 1
 
Last edited:
Upvote 0
I tested here and it worked.
Note that first you should ser the initial caption as a number: like 1. cannot start with "Hello".
 
Last edited:
Upvote 0
Hi
I just watched a video. He created a form then inserted a label and a command button. Then he made the label Caption =1, then clicked on command button and wrote the code below.
So when a user click the button the caption will increment by 1
Why is that? I changed the caption from 1 to Hello and the code stopped working. I thought the caption is a text, even when I type 1 it should be considered as a text, no? Thank you so much.

Code:
Private Sub myadd_Click()
myform.mycount.Caption = myform.mycount.Caption + 1
End Sub

Excel converts numbers stored in the textbox as numeric values, but the texts stored in the texbox can not convert them.


If you want to start the textbox with text like "Hello", then you could do something like this:

Code:
Private Sub myadd_Click()
    myform.mycount.Caption = Val(myform.mycount.Caption) + 1
End Sub


The Val( ) function converts the text to 0, but if a stored number exists then it leaves it as a numeric value.
 
Upvote 0
VBA tries to coerce the caption to a number because you are adding a number. If you were to use + "1" instead of + 1, you'd get a very different outcome. ;)
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

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