我正在尝试根据另一个工作表中的单元格值过滤工作表。相应的值是我的第二张表的第h列中的名称。
目前它过滤数据但只返回与filter_val
相关的1行数据,尽管有9行数据。
Sub filter_DMA_2() '' will be set to the combobox when that's figured out
Dim filter_val As String
Dim filter_range As Range
Dim Lrow As Long
Set Billed_xls = Workbooks("Copy of SWR1304 (Future Development Risk Assessment) Strathaven.xls").Sheets("Non Household Metered Users")
With Billed_xls
Lrow = .Range("a" & Rows.count).End(xlUp).Row
End With
'Set filter_range = Billed_xls.Sheets("Non Household Metered Users").Range("h:v")
Set filter_range = Workbooks("Copy of SWR1304 (Future Development Risk Assessment) Strathaven.xls").Sheets("Non Household Metered Users").Range("a2:v2" & Lrow)
'End With
filter_val = Workbooks("DMA_metered_tool_v1.xlsm").Worksheets("DMA list").Range("a1").Value
With filter_range
.AutoFilter
.AutoFilter Field:=5, Criteria1:=filter_val '.Value
End With
End Sub
知道如何解决这个问题,以便返回所有9行数据吗?
干杯
答案 0 :(得分:1)
可能是您的Lrow
计算被现有过滤器抛出 - 先清除:
Sub filter_DMA_2() '' will be set to the combobox when that's figured out
Dim filter_val As String
Dim filter_range As Range
Dim Lrow As Long
Set Billed_xls = Workbooks("Copy of SWR1304 (Future Development Risk Assessment) Strathaven.xls").Sheets("Non Household Metered Users")
With Billed_xls
' clear any existing filter to get accurate row count
.AutoFilterMode = False
Lrow = .Range("a" & .Rows.Count).End(xlUp).Row
Set filter_range = .Range("a2:v2" & Lrow)
End With
filter_val = Workbooks("DMA_metered_tool_v1.xlsm").Worksheets("DMA list").Range("a1").Value
filter_range.AutoFilter Field:=8, Criteria1:=filter_val
End Sub
filter_range.AutoFilter Field:=8, Criteria1:=filter_val
之前提到了错误的专栏。这已得到纠正,现在代码运行完美。