Excel 在另一张纸上
我目前正在开发一个用户表单。在此用户表单中,数据输入到textbox4中,数据通过基于Vlookup的commandbutton3放置在textbox6中。但是,vlookup必须从A:B范围内的工作表“DB-verzamelformulier”中检索其数据。目前,我收到错误消息:424 object required。有人能帮我写代码吗Excel 在另一张纸上,excel,vba,vlookup,Excel,Vba,Vlookup,我目前正在开发一个用户表单。在此用户表单中,数据输入到textbox4中,数据通过基于Vlookup的commandbutton3放置在textbox6中。但是,vlookup必须从A:B范围内的工作表“DB-verzamelformulier”中检索其数据。目前,我收到错误消息:424 object required。有人能帮我写代码吗 Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("DB - verzamelformulier") Wit
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
有趣的方法,但不能将公式分配给文本框,只能分配给单元格。尝试以下功能:
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的更改事件上调用它,以便无论何时更改,都会更新TextBox6的值
Private Sub TextBox4_Change()
TextBox6.Value = VerzamelFormulier(TextBox4.Value)
End Sub
使用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
你在查数字还是文字?您真的想要文本框中的公式还是文本框中公式的结果?谢谢您的帮助!由于更改功能,它的工作效果甚至比预期的更好