someone must know
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
Why not use a combobox containing only 20 and 40?
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
Ok I Need A Bit More Help Could You Give THe Whole Code Where I Would Put That In Please?
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
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
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...
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
Like this thread? Share it with others