简体   繁体   English

基于组合框选择的Excel VBA查找表值引起错误

[英]Excel VBA Lookup table value based on combobox selection Causing Error

I have a userform that has comboboxes that lookup a value in a table to populate a textbox on the same userform. 我有一个带有组合框的用户窗体,该组合框在表中查找值以在同一用户窗体上填充文本框。 The code is working. 该代码正在运行。 It produces the desired results and the form data saves accurately to the table, however instead of getting my MsgBox "Saved", I get error 1004 'Unable to get the VLookup property of the worksheet function class.' 它会产生所需的结果,并且表单数据会准确地保存到表中,但是,我没有得到MsgBox“已保存”,而是得到了错误1004“无法获取工作表函数类的VLookup属性”。 When I debug the line highlighted is Me.men10.Value = Application.WorksheetFunction.VLookup(rec1, rRange, 47, 0) 当我调试时突出显示的行是Me.men10.Value = Application.WorksheetFunction.VLookup(rec1, rRange, 47, 0)

This is the sub procedure in it's entirety: 这是整个子过程:

Private Sub men8_Change()
Dim rec1 As String
Dim rRange As Range
Set rRange = Sheets("Recipe Box").Range("tblRecipes")

rec1 = Me.men8.Value
Me.men10.Value = Application.WorksheetFunction.VLookup(rec1, rRange, 47, 0)

End Sub

I have four of these on the form and as I said, everything works as it should but still results in the error. 我在表单上有四个,而且正如我所说,一切都可以正常进行,但仍然会导致错误。 Any suggestions? 有什么建议么? I have searched everywhere but can't find anything related to errors on working code. 我到处搜索,但找不到与工作代码错误相关的任何内容。

Here is a snippet of the form controls: The circled textbox (men10) is populated from the combobox (men8) above it. 这是表单控件的片段:带圆圈的文本框(men10)是从其上方的组合框(men8)填充的。

I was able to replicate this, the error occurs if: - 我能够复制它,如果发生以下错误:

  • It does not find a match. 找不到匹配项。 In my scenario it was not concerned about upper or lower case but sure enough if there was no match I get the error you are getting. 在我的场景中,它并不关心大写或小写字母,但可以肯定的是,如果没有匹配项,我会得到您遇到的错误。 Do a manual check to ensure you are getting an exact match in the first column of the named range. 进行手动检查,以确保在指定范围的第一列中获得完全匹配的内容。
  • If I specified column number greater than the number of columns in the named range. 如果我指定的列号大于指定范围内的列数。 Remembering, a value of 1 does not strictly mean column A on the worksheet, but represent the first column in the named range (ie If the range is "C5:L200", then a value of 1 in Arg3 (col_index_num) would represent column C in the worksheet. 记住,值1并不严格表示工作表上的列A,而是表示指定范围内的第一列(即,如果范围为“ C5:L200”,则Arg3中的值1(col_index_num)将代表列)工作表中的C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM