[英]VBA Excel Userform error 1004
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. 现在,我需要使用vlookup查找我的第一个控件,文本框的其余部分将使用在组合框中键入的代码自动更新。 But the system shows error 1004. Can someone help me with this? 但是系统显示错误1004。有人可以帮助我吗? 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)
ComboBox.Value
提供选定的索引,但是您可以使用.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: Select Case
块可能是合适的:
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 最后,您可能还想使用ComboBox_code_Change()
事件处理程序,而不是ComboBox_code_AfterUpdate()
:前者将在每次组合框更改(即选择)时触发,而后者仅在组合框不再是活动控件时才触发,即您必须等待用户离开控件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.