Excel VBA获取值一张纸作为另一张纸上的search结果的参数

我有一张名为“lookup”的表格,在表格的B2单元格中,用户可以input一个部门代码(例如:190)并select一个“Search”button: 在这里输入图像说明

一旦他们点击searchbutton,它将把它们带到一个名为“department_lookup”的表中,A列中有部门代码190的账户代码,B列中有账户代码说明。 然而,在C1单元格中,我希望为查找单元格“department_lookup”中的查询可以刷新以显示正确的数据,来search填充单元格C1的值。 这是什么表“部门查找看起来像: 在这里输入图像说明

在A&B列将取决于有多less个帐户代码中有部门代码190的列表。

本质上,工作表department_lookup中的数据是一个dynamic查询,我希望单元格C1是参数值,它可以改变查询,以在工作表查找中显示用户在单元格B2中search的帐户代码。

以下是我用于表单查找的代码:

On Error GoTo Done: If Target.Column = 2 And Target.Cells.Count = 1 Then Cancel = True mycell = Sheets("department_lookup").Range("$C$1").Value If mycell = " " Then GoTo Done: Sheets("department_lookup").Activate End If Sheets("acct_codes").Visible = False Sheets("dept_list").Visible = False Cancel = True Application.ScreenUpdating = True End Sub 

这里是我有工作表department_lookup的vba:

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$C$1" Then With ActiveWorkbook.Connections("deptlookup").OLEDBConnection .CommandText = "select seg1_code+'-'+seg2_code+'-'+seg3_code+'-'+seg4_code as account_code, account_description from glchart as GL where GL.inactive_flag = 0 and seg2_code='" & Range("C1").Value & "' order by seg1_code" End With ActiveWorkbook.Connections("deptlookup").Refresh End If End Sub 

目前,当我手动将单元格C1的值更改为不同的部门代码时,department_lookup中的查询将更改为显示正确的代码,但是,我认为我的问题是正确设置C1等于用户在表单元格B2中search的任何用户查找 。 任何人都可以帮忙吗?

点击提交button,将该表的c1设置为等于初始工作表的单元格,然后按C1过滤,例如:

 Sheets("department_lookup").Cells(1, 3).Value = Sheets("lookup").Cells(2, 2).Value With Sheets("department_lookup") .Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=.Cells(1, 3).Value, VisibleDropDown:=True End With 

您可以将LR定义为最后一行,将LC定义为最后一列。