我目前正在处理用户表单。在此用户窗体中,通过基于Vlookup的命令按钮3将数据输入到textbox4中,并将数据放置在textbox6中。但是,vlookup必须从工作表“ DB-verzamelformulier”中检索范围为A:B的数据。当前,我收到错误消息:424 object required。有人可以帮我提供代码吗?
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("DB - verzamelformulier")
With ws
Texbox6.Formula = "VLookup(TextBox4.Value, DB - verzamelformulier!$A:$B), 2, False)"
End With
End Sub
答案 0 :(得分:1)
有趣的方法,但不能将公式分配给文本框,只能分配单元格。试试这样的功能:
Function VerzamelFormulier(LookUpValue As Variant) As Variant
Dim WS As Worksheet
Dim R As Range
Set WS = ThisWorkbook.Worksheets("DB - verzamelformulier")
Set R = WS.Range("A:A").Find(LookUpValue, LookIn:=xlValues, Lookat:=xlWhole)
If R Is Nothing Then
' The value wasn't found.
Else
' Return the value from the cell in the same row and column B.
VerzamelFormulier = WS.Cells(R.Row, 2)
End If
End Function
在TextBox4的change事件上调用它,以便每当更改TextBox6的值时就对其进行更新。
Private Sub TextBox4_Change()
TextBox6.Value = VerzamelFormulier(TextBox4.Value)
End Sub
答案 1 :(得分:0)
使用Vlookup:
Option Explicit
Sub test()
Dim varResults As Variant
varResults = Application.VLookup(TextBox4.Value, ThisWorkbook.Worksheets("Db - verzamelformulier").Range("A:B"), 2, False)
If Not IsError(varResults) Then
'If there is a results
TextBox6.Value = varResults
Else
'If there is no result
End If
End Sub