Evaluating a number in a textbox

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
If I wanted to test if the NUMBER in a textbox is greater than 50, which of the following is correct/accurate, or are they the same?

If Val(TextBox5) > 50 Then....

or

If TextBox5.Value > 50 Then ...

If neither are correct to evaluate a NUMBER in a textbox, then what is correct. thanks
 

Some videos you may like

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.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,696
Office Version
  1. 2010
Platform
  1. Windows
If I wanted to test if the NUMBER in a textbox is greater than 50, which of the following is correct/accurate, or are they the same?

If Val(TextBox5) > 50 Then....

or

If TextBox5.Value > 50 Then ...

If neither are correct to evaluate a NUMBER in a textbox, then what is correct. thanks
I would affix the Value property to that first one as well. Both of those will function the same. In the second one, VB will coerce the text number to a real number (automatically and behind the scene) in order to perform the mathematical comparison.
 

birdieman

Well-known Member
Joined
Jan 13, 2016
Messages
551
If all of my textboxes deal with numbers, would the above also apply when I am saving contents (which is always a number) to a spreadsheet, ie, use "textbox5.value" ?

Would I ever refer to a TextBox containing numbers as Textbox5.Text? (If this question has many "depends on" responses, no need to waste your time answering it.)

thanks for the response
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,696
Office Version
  1. 2010
Platform
  1. Windows
If all of my textboxes deal with numbers, would the above also apply when I am saving contents (which is always a number) to a spreadsheet, ie, use "textbox5.value" ?

Would I ever refer to a TextBox containing numbers as Textbox5.Text? (If this question has many "depends on" responses, no need to waste your time answering it.)
The contents of a TextBox is text, never numeric... VB coerces the text number to a real number when you use it in a mathematical operation so that it can complete the mathematical operation. As for the Text property question, the help files for the Text property says this in the Remarks section, "For a TextBox, any value you assign to the Text property is also assigned to the Value property" and, while it does not say so for the Value property explicitly, the reverse appears to be true as well.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,964
Messages
5,599,069
Members
414,281
Latest member
Engjamal2021

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
Top