Text format as percentage

  • Thread starter Thread starter Shane
  • Start date Start date
S

Shane

I have a userform that I use multiple times. When this form loads the first
time, I use the following code to format the text to a percentage:

Private Sub TextBox21_afterupdate()
If TextBox21.Value <> "" Then
TextBox21 = TextBox21 / 100
With UserForm1.TextBox21
.Text = Format(.Text, "0.00%")
End With
End If

however, if I ever focus back on that textbox, and then focus off, it
crashes the code. how would I add code that basically says "if the number is
already in percentage format, ignore the rest of this code"? (or is there a
better way to write the above code to fix this error all together?)

Thanks!
 
Try replacing the first line after the If statement with this one instead
(leave the rest of your code as is)...

TextBox21 = Replace(TextBox21, "%", "") / 100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top