2017-09-26 70 views
1

我有一张名为“查找”的纸张,并在该纸张的单元格B2中,用户可以输入部门代码(例如190)并选择“搜索”按钮: enter image description hereExcel VBA获取价值将一张纸作为另一张纸上的搜索结果的参数

一旦他们点击搜索按钮,它会带他们到一个叫纸“department_lookup”这列A与部门代码190帐户代码中,并在塔B账户代码说明。但是,在单元格C1中,我希望为查找单元格“department_lookup”中的查询可以刷新以显示正确数据的方式搜索填充单元格C1的值。这里是片“department_lookup”样子: enter image description here

在列A & B将取决于许多帐户代码怎么有部门代码在这190是一个列表。

本质上,在片材数据department_lookup是一个动态查询,我想小区C1到是改变查询以显示帐户码的参数值,该用户是在单元格B2在片材搜索查找

下面的代码我有片查找

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 

这里是VBA我有片department_lookup

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将更改为显示正确的代码,但是,我认为我的问题我s正确设置C1等于任何用户在单元格B2中查找的单元格查找。任何人都可以帮忙吗?

+0

只需将此行添加到您的搜索按钮的代码中: '工作表(“department_lookup”)。范围(“C1”)。值=工作表(“lookup”)。范围(“B2”)。值' – dwirony

回答

1

设置该表的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为最后一列。

+0

您使用单元格C1两次,在一张表中的值为B2 – Luuklag

+0

@Luuklag固定。还纠正了他的名单。 – Cyril

+0

@Cyril非常感谢您的回答。我应该把这段代码放在查找表中吗? – anve

相关问题