简体   繁体   English

使用双重变量条件自动过滤vba

[英]autofilter using double variable criteria vba

So I need to filter all the prices less or equal than the value of the cell(6,3) (which is 50.60). 因此,我需要过滤所有小于或等于cell(6,3)(即50.60)值的价格。

This is my code, but for some reason it just doesn't work for double values. 这是我的代码,但是由于某种原因,它对于双精度值不起作用。 For instance, if I change the value of the cell(6.3) from 50.60 to 50, the code works just fine. 例如,如果我将cell(6.3)的值从50.60更改为50,则代码可以正常工作。

Any tips? 有小费吗?

Sub cost()

Dim price As Double

'Application.DecimalSeparator = "."
'Application.ThousandsSeparator = ","
'Application.UseSystemSeparators = True

Range("B2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "0.00"

price = Cells(6, 3).Value

 Range("A1:C1").Select
 Selection.AutoFilter
 ActiveSheet.Range("$A$1:$C$12").AutoFilter Field:=1, Criteria1:="1"
 ActiveSheet.Range("$A$1:$C$12").AutoFilter Field:=3, Criteria1:="<=" & price
End Sub

This is my worksheet: 这是我的工作表:

hour consumption    price
1    20,50          0,00
1    50,10          1,30
1    70,60          1,50
1    10,70         30,60
1    15,90         50,60
1    30,80         56,20
1    60,30         60,30
2    45,20          0,00
2    80,60         11,30
2    50,70         20,40

Thanks for the help 谢谢您的帮助

With this line it works for me. 用这条线对我有用。 All you just have to do is converting price to string before you concatenate it to "<=" . 您只需要做的就是将price转换为string然后再将其连接为"<="

ActiveSheet.Range("$A$1:$C$12").AutoFilter Field:=3, Criteria1:="<=" & Str(price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM