I am creating a userform to update company data. The first control in my form is a combo box (which is code) and the rest are text box. Now I need to use vlookup to lookup my first control and the rest of the text box will be automatically updated with the code that I key in in my combo box. But the system shows error 1004. Can someone help me with this? Below is my combo box code:
Private Sub ComboBox_code_AfterUpdate()
'check if value exist
If WorksheetFunction.CountIf(Sheet1.Range("A:A"), Me.ComboBox_code.Value) = 0 Then
MsgBox "Incorrect Code"
Me.ComboBox_code.Value = ""
Exit Sub
End If
'lookup value based on first combobox
With Me
.TextBox_outlet = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 2, 0)
.TextBox_invoice = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 3, 0)
.TextBox_sales = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 4, 0)
.TextBox_comm = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 5, 0)
.TextBox_gst = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 6, 0)
.TextBox_netsales = Application.WorksheetFunction.VLookup((Me.ComboBox_code), Sheet1.Range("Code"), 7, 0)
End With
End Sub
ComboBox.Value
gives the selected index, but you can get the selected item with .List(.ListIndex)
Private Sub ComboBox_code_AfterUpdate()
Dim c As Range
Set c = Sheet1.Range("Code").Resize(, 1).Find(Me.ComboBox_code.List(Me.ComboBox_code.ListIndex), , , xlWhole)
If c Is Nothing Then
MsgBox "Incorrect Code"
Me.ComboBox_code.ListIndex = -1
Else
Me.TextBox_outlet.Text = c(, 2)
Me.TextBox_invoice.Text = c(, 3)
Me.TextBox_sales.Text = c(, 4)
Me.TextBox_comm.Text = c(, 5)
Me.TextBox_gst.Text = c(, 6)
Me.TextBox_netsales.Text = c(, 7)
End If
End Sub
you should also test for any actual combobox value selection made by the user
a Select Case
block could be appropriate:
Private Sub ComboBox_code_AfterUpdate()
Dim codeRow As Long
Dim codeRng As Range
Set codeRng = Sheet1.Range("Code")
With Me
Select Case True
'check if user selected a value
Case .ComboBox_code.ListIndex = -1
MsgBox "No Code selected!", vbCritical
.ComboBox_code.Value = ""
'check if value exist
Case WorksheetFunction.CountIf(codeRng.Resize(, 1), .ComboBox_code.Value) = 0
MsgBox "Incorrect Code", vbCritical
.ComboBox_code.Value = ""
Case Else
codeRow = WorksheetFunction.Match(.ComboBox_code.Value, Sheet1.Range("Code"), 0) 'lookup value based on first combobox
.TextBox_outlet = codeRng.cells(codeRow, 2)
.TextBox_invoice = codeRng.cells(codeRow, 3)
.TextBox_sales = codeRng.cells(codeRow, 4)
.TextBox_comm = codeRng.cells(codeRow, 5)
.TextBox_gst = codeRng.cells(codeRow, 6)
.TextBox_netsales = codeRng.cells(codeRow, 7)
End Select
End With
End Sub
Finally, you may also want to use the ComboBox_code_Change()
event handler instead of ComboBox_code_AfterUpdate()
: the former would fire at every combobox change (ie selection) while the latter would fire only once the combobox is no longer the active control ie you must wait for the user to leave the control
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.