[英]vba: using combobox value as autofilter criteria
I'm trying to use a value from a Combo Box with a list of names as criteria of an AutoFilter and then copy the results from my database (BASE_ACOMPANHAMENTOS) to my results sheet (BUSCA). 我试图使用带有名称列表的组合框中的值作为自动筛选的条件,然后将结果从数据库(BASE_ACOMPANHAMENTOS)复制到结果表(BUSCA)。 My Combo Box name is busca_lista and this is what I've tried so far: 我的组合框名称是busca_lista ,这是我到目前为止尝试过的操作:
Private Sub OKBUSCA_Click()
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim lstrw As Long
Set ws = Sheets("BASE_ACOMPANHAMENTOS")
Set ws1 = Sheets("BUSCA")
lstrw = ws.Cells(Rows.Count, 2).End(xlUp).Row
ws1.Range("C12:H100").Clear
ws1.Range("X5").Value = busca_lista.Value
ws.Range("A1:F" & lstrw).AdvancedFilter Action:= _
xlFilterCopy, CriteriaRange:=ws1.Range("X5"), CopyToRange:=ws1.Range("C11:H11"), Unique:=False
End Sub
As you guys can see, I'm copying the value from my Combo Box to one cell and then using this value as a criteria, but it isn't working because it's showing all the names in my database, not the name that I've searched for. 如你们所见,我将值从“组合框”复制到一个单元格中,然后使用该值作为标准,但它不起作用,因为它显示了数据库中的所有名称,而不是我ve搜索。 You can see the example below: 您可以看到以下示例:
The form: 表格:
Your CriteriaRange
needs to include both column heading and values. 您的CriteriaRange
需要同时包含列标题和值。 Based on your code, I've added the heading "Nome" in X4
. 根据您的代码,我在X4
添加了标题“ Nome”。 Your criteria range is X4:X5
. 您的标准范围是X4:X5
。
Private Sub OKBUSCA_Click()
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim lstrw As Long
Set ws = Sheets("BASE_ACOMPANHAMENTOS")
Set ws1 = Sheets("BUSCA")
lstrw = ws.Cells(Rows.Count, 2).End(xlUp).Row
ws1.Range("C12:H100").Clear
ws1.Range("X4").Value = "Nome"
ws1.Range("X5").Value = busca_lista.Value
ws.Range("A1:F" & lstrw).AdvancedFilter Action:= _
xlFilterCopy, CriteriaRange:=ws1.Range("X4:X5"), CopyToRange:=ws1.Range("C11:H11"), Unique:=False
End Sub
You have given the value to filter on but not the column. 您已提供要过滤的值,但没有提供该列。 You need to have a cell which also holds the value Nome in in X4 and have your criteriarange as X4:X5 您需要具有一个在X4中也包含Nome in值的单元格,并且您的条件范围为X4:X5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.