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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define Case as "#.##%" 2

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
I am in the process of creating a userform. A textbox on the form is used for a percentage. The textbox is initialized as "%".

The following code results in the textbox being able to be exited in percentage format, as well as if they blank out the textbox or take no action prior to exiting the textbox, it returns to "%".

What I need is to know how to define within the case if the user gives the textbox a value, say "4.05%", then exits the textbox...then for whatever reason reenters and reexits without changing anything. Therefore "4.05%" is not currently defined within my case. Is it possible to do this with some sort of numeric variable (e.g. "#.##%")? If so, how would it be displayed?

Thanks for the help!!

Code:
Private Sub UserForm_Initialize()

    txtRT11.Value = "%"

End Sub


Private Sub txtRT11_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Select Case txtRT11
        Case ""
            txtRT11 = "%"
        Case "%"
            txtRT11 = "%"
        Case Else
            txtRT11.Text = Format(txtRT11 * 0.01, "percent")
    End Select

End Sub
 
Hi,
Code:
        Case Else
            txtRT11.Text = Format(txtRT11 / 100, "#.00%")

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for the reply Skip!

Unfortunately, that did not work.

Same error occurs and that line is called into question. It is still not accepting me exiting, I am assuming, since the % character is present with a value in front of it.

Any other ideas?

Thanks!
 


please explain in more exacting detail, as it worked perfectly in my own test.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here is my code...

Code:
Private Sub txtRT11_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Select Case txtRT11
        Case "", "%"
            txtRT11 = "%"
        Case Else
            txtRT11.Text = Format(txtRT11 / 100, "#.00%")
    End Select

End Sub

...if a value is already entered into the textbox, Ex: 4.05%, then the textbox is reentred and reexited without any change being made, I get the following error message, which upon 'debugging' highlights the line of code that you suggested.

"Run-time error '13':
Type mismatch

I have even attempted disabling any other code regarding this textbox and still get the error.
 
Code:
        Case Else
            If IsNumeric(txtRT11.Value) Then _
                txtRT11.Text = Format(txtRT11 / 100, "#.00%")

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Code:
Private Sub txtRT11_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Select Case txtRT11
        Case "", "%"
            txtRT11 = "%"
        Case Else
            txtRT11.Text = Format(CDbl(Replace(txtRT11, "%", "")) / 100, "#.00%")
    End Select

End Sub

combo
 


Nice one, Glenn ==> [purple]*[/purple]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



Geez, I can't attribute THAT one to a typo! THAT was a combo error!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top