Textbox Question

will2learn

Board Regular
Joined
Dec 1, 2005
Messages
144
I have a userform with various textboxes on it, which inturn feeds the data into a spreadsheet. One of the boxes is used to record messages/notes and is causing a problem as the users sometimes enter data with a ",".

Is there away in which I can stop the user entering a "," as they are typing?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Haratio

New Member
Joined
Jun 15, 2007
Messages
40
Will2learn,

what about something like this,

Private Sub TextBox1_Change()
If TextBox1.Text = "," Then
MsgBox "please dont do that"
TextBox1 = Null
End If
End Sub


or if not this then something along these lines.

Haratio
 
Upvote 0

will2learn

Board Regular
Joined
Dec 1, 2005
Messages
144
Your suggestion works if only "," is entered in the text box. I need it to throw up an error if something like this is entered "hello, everyone". As the user enters the "," I need away to stop them entering the ",".
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Why do you need to prevent the user entering a comma?
 
Upvote 0

will2learn

Board Regular
Joined
Dec 1, 2005
Messages
144
The data needs to be used in a csv file. If the "," is entered then the data is out of sync.
 
Upvote 0

blazonQC

Board Regular
Joined
Jun 6, 2007
Messages
92
Code:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 188 Then
TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
End If
End Sub

This isn't a fantastic way of doing it..... but it works. And 1 real drawback is that if a user holds the ',' key down then it won't work properly.... but who's going to do that?

I'll try to come up with a better method for you that updates faster. Or you could just go ahead and parse the strings before moving them.

Chad
 
Upvote 0

will2learn

Board Regular
Joined
Dec 1, 2005
Messages
144
Chad,

Thanks for your code That seems to have done the trick. I've added a warning message so that the user knows not to use the comma.
 
Upvote 0

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

This will cater for the event that the user pastes in text with a comma:
Code:
Private Sub TextBox1_Change()
If InStr(TextBox1.Text, ",") <> 0 Then
    With Application
        .EnableEvents = False
        TextBox1.Text = Replace(TextBox1.Text, ",", " ")
        .EnableEvents = True
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,190,774
Messages
5,982,842
Members
439,799
Latest member
matts12

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top