Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Validate Text in Textbox to Ensure Text is a Valid Price Format

I have several textboxes on several userforms that I want to test the
contents of the textboxes to ensure a properly formatted price is entered
like this "#,###.##" or "####.##". This is the Sub that I use for that and
it works great. My question is, is there a easier way or cleaner coded way
to do this?

Sub ValidatePrice(strPrice As String, strControlName As String)
' is called to ensure proper format of a price entered in a textbox

Dim strChars As String

' format price first
strPrice = Format(strPrice, "#,###.00")

' remove unwanted characters (myChars) from price
strChars = "$, "
For i = 1 To Len(strChars)
strPrice = Replace(strPrice, Mid(strChars, i, 1), "")
Next i

' ensure first number is 1-9
If Not Left(strPrice, 1) Like "[1-9]" Then
GoTo InvalidPrice
End If

' "." should be in the thrid position ####.##
If Mid(strPrice, Len(strPrice) - 2, 1) < "." Then
GoTo InvalidPrice
End If

' scan each positon in Price string except position where period
' to ensure position value is numeric
For i = Len(strPrice) To 1 Step -1
If i < Len(strPrice) - 2 And Not Mid(strPrice, i, 1) Like "[0-9]"
Then
GoTo InvalidPrice
End If
Next i

Exit Sub

InvalidPrice:

StopCode = True
strPrompt = "Problem"
intButtons = vbCritical
strTitle = "Please enter a valid price for " & strControlName & " in
this format: #,###.##"
MsgBox strTitle, intButtons, strPrompt

End Sub
--
Cheers,
Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Validate Text in Textbox to Ensure Text is a Valid Price Format

This is untested, but I believe it will work correctly and is how I would
structure the code to do what your code does...

' Called to ensure proper format of a price entered in a textbox
Sub ValidatePrice(strPrice As String, strControlName As String)
Dim X As Long, strChars As String

' remove unwanted characters (myChars) from price
strChars = "$, "
For X = 1 To Len(strChars)
strPrice = Replace(Mid(strPrice, X, 1), "")
Next

' format strPrice
strPrice = Format(strPrice, "0.00")

' display error if strPrice is not a valid number
If Value Like "*[!0-9.]*" Or Value Like "*.*.*" Or Len(Value) = 0 _
Or Value = "." Or Not Value Like "*.##" Then
strPrompt = "Problem"
intButtons = vbCritical
strTitle = "Please enter a valid price for " & strControlName & _
" in this format: #,###.##"
MsgBox strTitle, intButtons, strPrompt

' optional section... strPrice is correctly entered
Else
'
' use this section to give strPrice a final format, perhaps this?
'
' strPrice = Format(strPrice, "$#,##0.00")
'
End If
End Sub

--
Rick (MVP - Excel)


"Ryan H" wrote in message
...
I have several textboxes on several userforms that I want to test the
contents of the textboxes to ensure a properly formatted price is entered
like this "#,###.##" or "####.##". This is the Sub that I use for that
and
it works great. My question is, is there a easier way or cleaner coded
way
to do this?

Sub ValidatePrice(strPrice As String, strControlName As String)
' is called to ensure proper format of a price entered in a textbox

Dim strChars As String

' format price first
strPrice = Format(strPrice, "#,###.00")

' remove unwanted characters (myChars) from price
strChars = "$, "
For i = 1 To Len(strChars)
strPrice = Replace(strPrice, Mid(strChars, i, 1), "")
Next i

' ensure first number is 1-9
If Not Left(strPrice, 1) Like "[1-9]" Then
GoTo InvalidPrice
End If

' "." should be in the thrid position ####.##
If Mid(strPrice, Len(strPrice) - 2, 1) < "." Then
GoTo InvalidPrice
End If

' scan each positon in Price string except position where period
' to ensure position value is numeric
For i = Len(strPrice) To 1 Step -1
If i < Len(strPrice) - 2 And Not Mid(strPrice, i, 1) Like "[0-9]"
Then
GoTo InvalidPrice
End If
Next i

Exit Sub

InvalidPrice:

StopCode = True
strPrompt = "Problem"
intButtons = vbCritical
strTitle = "Please enter a valid price for " & strControlName & " in
this format: #,###.##"
MsgBox strTitle, intButtons, strPrompt

End Sub
--
Cheers,
Ryan


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accounting Format Text in a Textbox RyanH Excel Programming 4 September 10th 08 01:05 PM
how to format text in textbox? ghost Excel Discussion (Misc queries) 1 May 18th 08 10:41 AM
Format text in a textbox DB Excel Programming 3 February 9th 06 03:46 PM
how to format a date/validate for a text box entry on a user form Tom Ogilvy Excel Programming 3 June 1st 05 05:06 PM
Qn: Ensure Number is valid for a circle?? Michael Vaughan Excel Programming 3 August 20th 04 07:52 AM


All times are GMT +1. The time now is 02:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"