I've Made A User Form And I Need Help With Validation

Morrissey

Board Regular
Joined
Mar 8, 2002
Messages
85
Hi, everyone I've got yet another problem with my data form that I've made In VBA, I Want A Text Box Which Data Is Entered To Only Allow The Following values "20" and "40" And If Any Other Is Entered A Error Message Coming Up Saying "You Can Only Enter 20 And 40!" Here the Code I Currently Have And Would Appreciate Some Help Thank You! :)


Private Sub txtContainerType_Change()
Sheet1.Range("B19").Value = txtContainerType
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Further to this, in order to properly validate it so they cant type anything different into it, in the combobox's properties window set the following:

Match Required - TRUE
Style - 2fmStyleDropDownList

Now they'll only be able to select exactly what's in your list, plus if they enter 2, it'll automatically complete the entry to 20 and the same for 40
 
Upvote 0
OK, draw yourself a Combobox in your form. Now double click on the form itself, NOT one of the objects in it. Now change:

Private Sub UserForm_Click()
to
Private Sub Userform_Initialize()

Underneath that put in these two lines of code:

Combobox1.AddItem "20"
Combobox1.AddItem "40"

Now go back to the Userform and click once on the Combobox. In the properties window go down until you find Match Required and change it to TRUE, now find STYLE and change that to 2fmDropDownList

Now try that out and it should work fine. They dont even have to click on the arrow if they down want to. Simply type 2 or 4 and it'll auto-complete

Hope this helps
 
Upvote 0
Hi! it sort of helps however My combo box isn't under the click button here is all my code for the thing I hope this helps you more to help me sorry I suck @ VB

Private Sub cmdEnterInvoiceData_Click()

On Error GoTo WrongInput
Sheet1.Range("b11").Value = CDbl(txtInvoiceNum.Text)
Exit Sub

WrongInput:
MsgBox "Please enter a number"
txtInvoiceNum.Text = ""
txtInvoiceNum.SetFocus

End Sub



Private Sub txtContainerType_Change()
Sheet1.Range("B19").Value = txtContainerType
Combobox1.AddItem "20"
Combobox1.AddItem "40"

End Sub

Private Sub txtEnterREF_Change()
Sheet1.Range("E11").Value = txtEnterREF
End Sub

Private Sub txtTo_Change()
Sheet1.Range("B15").Value = txtTo
End Sub

Private Sub txtVessel_Change()
Sheet1.Range("B16").Value = txtVessel
End Sub

Private Sub UserForm_Click()

End Sub
 
Upvote 0
Beautiful Soundman, just beautiful! You solved a problem of mine with that Private Sub Userform_Initialize() thing, without knowing it. Care to take a glimpse at my SendKey prob while you're at it? It´s down the line here somewhere...
 
Upvote 0
Tell you what Morrisey, I'll email you a sample I just did. If it's no good, then send me your form (if you can) back via email, and I'll take a look
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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
Back
Top