VBA question - Percentages in text box

A

ajliaks

Hi

I need to input data in percentage format, like this: 50.5%

Which code can I use? I also want to prevent user of inputing wron
data.

Private Sub PercentTB_KeyPress(ByVal KeyAscii A
MSForms.ReturnInteger)
If ((KeyAscii < Asc("0")) Or (KeyAscii > Asc("9"))) Then
If (KeyAscii < 0) Or (KeyAscii > 1) Then
KeyAscii = 0
End If
End If
'MsgBox PercentTB.Value
End Su
 
T

Tom Ogilvy

Private Sub PercentTB_Exit(ByVal Cancel As MSForms.ReturnBoolean)
PercentTB.Text = Format(csng(PercentTB.Text),"#0.#%)
End Sub

Private Sub PercentTB_KeyPress(ByVal KeyAscii As _
MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
if KeyAscii <> asc(Application.international( _
xlDecimalSeparator)) then ' allow decimal separator
KeyAscii = 0
End If
End If
'MsgBox PercentTB.Value
End Sub

Not sure what values you want to allow. Will they enter a percent or a
decimal?
 

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

Top