在VBA UserForm中基于多个变量查找数据

时间:2016-09-05 03:18:03

标签: excel vba excel-vba userform

我在vba中创建了一个UserForm,如下所示: enter image description here

用户填写表单后,记录将保存在" Sheet2"中。对于每个控件ID,应该有3个可能的活动。我想要做的是,在用户输入控件ID和活动名称后,表单将填充来自" Sheet2"的现有数据。以下是我使用"索引" /"匹配"但它返回"类型不匹配"错误。

<div ng-repeat="value in values track by $index">
        <a ng-click="function(value)">{{ value }}</a>
</div>

Reg1是位于第2列的控制ID; Reg2是全名,位于第3栏;和 Reg3是位于第4栏的活动。

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:1)

托马斯有一个很好的解决方案。如果您只想使用VBA(没有Excel功能),您还可以设置如下搜索循环:

With ws
    For row = fisrtRow To lastRow
        val = .Cells(row, col)
        If val = controlID And val = activity Then
            '....
        Else
            '...
        End If
    Next row
End With

答案 1 :(得分:0)

这样的事情可以让你走上正轨

                            '-- lets assume you want value from row 1, column 3
                            '   from "Sheet2"
Dim wb As Workbook
Dim ws As Worksheet

Dim row As Integer, column As Integer

Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet2")
row = 1
col = 3

Me.Reg2.Text = ws.Cells(row, col)

答案 2 :(得分:0)

here is the best solution for you


write the following code ih the after update event

Private Sub Reg3_AfterUpdate()
SheetS("SHEET2").Range("A1:C4").Select
 Selection.AutoFilter
    SheetS("SHEET2").Range("$A$1:$C$4").AutoFilter Field:=1, Criteria1:="=Product no",  Operator:=xlAnd

    SheetS("SHEET2")..Range("$A$1:$C$4").AutoFilter Field:=1, Criteria1:="=Activity",  Operator:=xlAnd

'add your code to get the value from range(a2:c2) like the following
FULLNAME.TEXT=SHEETS("sheet2").range("B2)
End Sub



'in this example I assume that the data is stored in range(A1:C4) 
'SO AFTER YOU APPLY THE FILTER YOU WILL GET ONE UNIQUE ROW  REPRESENTING 
'YOUR 'DATA  .THEN YOU JUST NEED TO IMPORT THE DATA FROM THE sheet2 CELLs

'and so on