【问题标题】:Vlookup in another sheet在另一个工作表中查找
【发布时间】:2019-04-08 16:19:01
【问题描述】:

我目前正在处理用户表单。在这个用户表单中,数据输入到 textbox4 中,数据通过基于 Vlookup 的 commandbutton3 放置到 textbox6 中。但是,vlookup 必须从 A:B 范围内的工作表“DB - verzamelformulier”中检索其数据。目前我收到错误消息:需要 424 个对象。有人可以帮我写代码吗?

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

【问题讨论】:

  • 您是在查找数字还是文本?你真的想要文本框中的公式还是文本框中公式的结果?

标签: excel vba vlookup


【解决方案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 的更改事件上调用它,以便每当它发生更改时,TextBox6 的值都会更新。

Private Sub TextBox4_Change()
    TextBox6.Value = VerzamelFormulier(TextBox4.Value)
End Sub

【讨论】:

  • 感谢您的帮助!由于更改功能,它的效果甚至比预期的要好
【解决方案2】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多