在VBA UserForm中查找基于多个variables的数据

我已经在vba中创build了一个UserForm,如下所示: 在这里输入图像说明

一旦用户填写完表格,logging将保存在“Sheet2”上。 对于每个控制ID,应该有3个可能的活动。 我想要做的是在用户input控件ID和活动名称后,表单将填充来自“Sheet2”的现有数据。 这是我使用“索引”/“匹配”,但它返回与“types不匹配”错误。

Private Sub Reg3_AfterUpdate() With Me .Reg2 = Application.WorksheetFunction.Index(Sheet2.Columns(3), Application.WorksheetFunction.Match(1, (Sheet2.Columns(2) = CLng(Me.Reg1)) * (Sheet2.Columns(4) = CLng(Me.Reg3)), 0)) End With End Sub 

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

有人可以帮帮我吗?

Thomas有一个很好的解决scheme。 如果你只想使用VBA(没有Excel函数),你也可以像这样设置search循环:

 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 

像这样的东西可以让你走上正轨

  '-- 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) 
 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