Vba textbox control text box in Excel
The Textbox control is used to get input from the user.
The textbox control can be used to get alphabets,numerics and other symbols.
There will hardly be a form without a textbox control.
This is a basic control and a must needed for obtaining user input.
Like other controls textbox also has various properties as explained in the previous chapters.
Some of the distinct properties are discussed below
1.PasswordChar property
This property is used to set the password character of the textbox, when used to obtain password from the user.
2.Multiline property
This property specifies whether the textbox can get multiline input.
This takes a binary value,true or false.
Text Property
This property is used to get or set the text in the textbox control.
Events of Textbox control
The events associated with textbox controls are as discussed below:
1.Change event:
This event is triggered whenever the text in the textbox changes.
The following code illustrates this
- Private Sub TextBox1_Change()
- MsgBox "Changed"
- End Sub
2.KeyDown Event
This event is triggered whenever a text is entered in the textbox.
This event is triggered the moment a key is pressed.
For example, the following snippet checks the text input and triggers an action whenever key "a" is pressed.
- Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- If KeyCode = vbKeyA Then
- MsgBox "You Pressed A"
- End If
- End Sub
3.KeyUp Event
This event is same as the Keydown, except that, this event is triggered at the moment of key release.
The follwing code illustrates this; with the same logic as above.
- Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- If KeyCode = vbKeyA Then
- MsgBox "You Pressed A"
- End If
- End Sub