Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Data Text Validation

Status
Not open for further replies.

mrteacher

IS-IT--Management
Nov 13, 2002
68
US
I have VBA code to enter in AnnualSalary but am stuck in validating so not to have Text or Enter Key error out - so user has to re-enter.

AnnualSalary = InputBox("Annual Salary? Example: 65000", "Enter")
A Do While AnnualSalary <= 0
Loop
Range(&quot;D6&quot;).Value = (Val(AnnualSalary))

The &quot;Val&quot; works to translate a string to show $0.00 in the cell - but I want to have the user have to re-enter if a &quot;string&quot; or &quot;ENTER&quot; key is used.

I tried the following below the AnnualSalary = InputBox(&quot;Annual Salary? Example: 65000&quot;, &quot;Enter&quot;) --- but it is not working. I also tried adding: Dim AnnualSalary As Integer at the top.

If AnnualSalary < Asc(&quot;0&quot;) Or AnnualSalary > Asc(&quot;9&quot;) Then
' AnnualSalary = 0
' End If
' Const ENTER As Integer = 13 ' the ANSI value of the Enter Key
' If AnnualSalary = ENTER Then
' AnnualSallary = 0
' End If

What am I missing here?
 
Personally I would let the error handler deal with figuring out if it is ok. There are probably many other ways of doing it but you can try something like this:

Do While AnnualSalary <= 0
entersal:
SalaryStr = InputBox(&quot;enter salary&quot;, &quot;salary input&quot;)

On Error GoTo badsal
AnnualSalary = CCur(SalaryStr)
Loop
On Error GoTo 0

Cells(6, 4) = AnnualSalary
Exit Sub

badsal:
MsgBox &quot;Enter the number that is the person's salary&quot;, vbOKOnly
Resume entersal


Peter Richardson
 
Thanks Peter,

I will take a look at these solutions.

Tek-Tips has helped me so often. This is a valuable tool for many people.

~Steve~
 
ya...i agree with u , Steve...
this is certainly a good place to learn from Experts in VB..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top