简体   繁体   English

VBA 表格 - Vlookup 单元格并为该单元格赋值

[英]VBA Form - Vlookup cell and assign value to that cell

Encountering an issue in a VBA regarding vlookup function.在 VBA 中遇到有关 vlookup function 的问题。

I have 2 comboboxes and 6 Textboxs for user input.我有 2 个组合框和 6 个文本框供用户输入。

I want to use a vlookup (or index,Match(),Match()) to look up a cell in a data.table and assign the values from the textboxes to these cells.我想使用 vlookup(或索引、Match()、Match())来查找 data.table 中的单元格并将文本框中的值分配给这些单元格。

When I run the code for what I believe should work, it is returning object errors.当我运行我认为应该有效的代码时,它返回 object 错误。

Private Sub CommandButton2_Click()

Dim MonthlyTable As Range
Set MonthlyTable = Sheets("DATA Monthly").Range("A6:AE400")
Dim ColumnRef As Range
Set ColumnRef = Sheets("Drivers").Range("N11")

' Assign CB2 value to M11 cell reference so it can be converted to a column ref in N11.
Sheets("Drivers").Range("M11").Value = ComboBox2.Value

Dim CB1Value As String
CB1Value = "Joiners" & ComboBox1.Value
Dim CB2Value As String
CB2Value = ComboBox2.Value

MsgBox CB1Value & " " & CB2Value

Dim tb1value As Range
tb1value = Application.WorksheetFunction.VLookup(CB1Value, MonthlyTable, ColumnRef, False)
tb1value.Value = TextBox1.Value

Unload Me
End Sub

I am at a loss for what to do here as I feel like it should be this simple!我不知道在这里做什么,因为我觉得它应该这么简单!

Thanks in advance.提前致谢。

Edit.编辑。 Further digging indicates that you cannot select a cell you are vlookup'ing as this commands only returns a value it does not actually select the cell for my intents and purposes.进一步挖掘表明你不能 select 一个你正在查找的单元格,因为这个命令只返回一个值它实际上不是 select 单元格出于我的意图和目的。

not really clear to me you actual aim, but just following up your desire as stated by: 对我来说,您的真正目标还不是很清楚,但只是按照下面的描述继续您的愿望

I want to use a vlookup (or index,Match(),Match()) to look up a cell in a data table and assign the values from the textboxes to these cells 我想使用vlookup(或index,Match(),Match())在数据表中查找单元格,并将文本框中的值分配给这些单元格

you may want to adopt the following technique: 您可能要采用以下技术:

Dim tb1value As Variant '<--| a variant can be assigned the result of Application.Match method and store an error to be properly cheeked for 
tb1value = Application.Match(CB1Value, MonthlyTable.Column(1), 0) '<--| try finding an exact match for 'CB1Value' in the first column of your data range 
If Not IsError(tblvalue) Then MonthlyTable(tb1value, columnRef.Value).Value = TextBox1.Value '<--| if successful then write 'TextBox1' value in data range cell in the same row of the found match and with `columnRef` range value as its column index

Excel uses worksheet functions to manipulate data, VBA has different tools, and when you find yourself setting cell values on a sheet via VBA so that some worksheet function can refer to them it is time to look for a true VBA solution. Excel使用工作表函数来处理数据,VBA具有不同的工具,当您发现自己通过VBA在工作表上设置单元格值时,以便某些工作表函数可以引用它们,是时候寻找真正的VBA解决方案了。 I suggest the following which, by the way, you might consider running on the Change event of Cbx2 instead of a command button. 我建议以下建议,顺便说一句,您可以考虑在Cbx2的Change事件上而不是在命令按钮上运行。

Private Sub Solution_Click()
    ' 24 Mar 2017

    Dim MonthlyTable As Range
    Dim Rng As Range
    Dim Lookup As String
    Dim Done As Boolean

    Set MonthlyTable = Sheets("DATA Monthly").Range("A2:AE400")
    ' take the lookup value from Cbx1
    Lookup = ComboBox1.Value

    Set Rng = MonthlyTable.Find(Lookup)    
    If Rng Is Nothing Then
        MsgBox Chr(34) & Lookup & """ wasn't found.", vbInformation, "Invalid search"
    Else
        With ComboBox2
            If .ListIndex < 0 Then
                MsgBox "Please select a data type.", vbExclamation, "Missing specification"
            Else
                TextBox1.Value = MonthlyTable.Cells(Rng.Row, .ListIndex + 1)
                Done = True
            End If
        End With
    End If

    If Done Then Unload Me
End Sub

There are two points that need explanation. 有两点需要解释。 First, the form doesn't close after a rejected entry. 首先,拒绝输入后,表单不会关闭。 You would have to add a Cancel button to avoid an unwanted loop where the user can't leave the form until he enters something correct. 您必须添加一个“取消”按钮,以避免不必要的循环,在这种循环中,用户只有输入正确的内容才能离开表单。 Note that Done is set to True only when the search criterion was found And a value was returned, and the form isn't closed until Done = True . 请注意,仅当找到搜索条件并返回值时,将Done设置为True,直到Done = True ,表单才会关闭。

Second, observe the use of the ListIndex property of Cbx2. 其次,观察Cbx2的ListIndex属性的使用。 All the items in that Cbx's dropdown are numbered from 0 and up. Cbx下拉菜单中的所有项目均从0开始编号。 The ListIndex property tells which item was selected. ListIndex属性告诉您选择了哪个项目。 It is -1 when no selection was made. 未选择时为-1。 If you list the captions of your worksheet columns in the dropdown (you might do this automatically when you initialise the form) there will be a direct relationship between the caption selected by the user (such as "Joiners") and the ListIndex. 如果您在下拉列表中列出工作表列的标题(初始化表单时可能会自动执行此操作),则用户选择的标题(例如“ Joiners”)与ListIndex之间将存在直接关系。 The first column of MonthlyTable will have the ListIndex 0. So you can convert the ListIndex into a column of MonthlyTable by adding 1. MonthlyTable的第一列的ListIndex为0。因此,您可以通过添加1将ListIndex转换为MonthlyTable的列。

I think it is better to use "find" in excell vba to select a cell instead of using vlookup or other methods.我认为最好在 excell vba 到 select 单元格中使用“查找”而不是使用 vlookup 或其他方法。

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

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