简体   繁体   English

Excel VBA-筛选值

[英]Excel VBA - Filtering Values

I'm writing a code in VBA to filter my data in a column in order to perform regression. 我正在VBA中编写代码以过滤列中的数据以执行回归。 Down below is a snapshot of the data. 下面是数据的快照。 I am trying to filter the data such that I am going from the most recent date (Jan-13) and backwards trying to find values with greater than 10% change. 我正在尝试过滤数据,以便从最近的日期(1月13日)开始,然后向后尝试查找变化大于10%的值。 By change I mean, (Absolute value (Recent_Date_Rate - Older_Date_Rate))/Recent_Date_Rate > 10%. 我所说的变化是,(绝对值(Recent_Date_Rate-Older_Date_Rate))/ Recent_Date_Rate> 10%。

I have accomplished writing a code that would give me all the noise values (bad numbers with less than <10% change in steam rate). 我已经完成了编写将给我所有噪声值(蒸汽速率变化小于10%的不良数字)的代码。 All it does is, compare the most recent values to the older values and if the change is less than 10% it copies and pastes the value in column R. 它所做的就是将最新值与旧值进行比较,如果更改小于10%,它将复制并将该值粘贴到R列中。

My question is really, is there a way for make the code do the opposite? 我的问题是,有没有办法使代码相反? so that only the good values that I intend to use for my regression are printed in column R. I tried changing the operator (>) to this, but that didnt work. 因此,只有我打算用于回归的良好值才会显示在R列中。我尝试将运算符(>)更改为该值,但这没有用。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you very much in advance. 提前非常感谢您。 Any tips to improve the code would also be appreciated. 任何改进代码的技巧也将不胜感激。

Sub main6()

    Range("R:R").ClearContents

    Dim offset1 As Integer
    Dim offset2 As Integer

    For j = Range("P1").Value To 3 Step -1
        offset1 = Cells(j, "P").Row - 3
        offset2 = Range("P1").Value - Cells(j, "P").Row
        For k = 1 To offset1
            If Abs((Cells(j, "P").Value - Cells(k + 2, "P").Value)) / Cells(j, "P").Value < 0.05 Then
                Cells(k + 2, "P").Copy
                Cells(k + 2, "R").PasteSpecial Paste:=xlValues
            End If
        Next k
    Next j
End Sub

在此处输入图片说明

If I got you right, your formula abs(xy)/x > 0.1 goes for a direct percentage. 如果我理解正确,则您的公式abs(xy)/x > 0.1可以直接得出百分比。 In theory it is like x*1.1 < y OR x/1.1 > y . 理论上,它就像x*1.1 < y OR x/1.1 > y While there is the chance that x will be in range for y but y not for x (from 19.4 the difference to 21.5 will be bigger than 10% but the opposite will be less then 10%). 尽管x可能在y的范围内,但y不在x的范围内(从19.4到21.5的差将大于10%,而相反的则小于10%)。 Still you are just looking into the past. 不过,您只是在回顾过去。 This way the first value will always be shown. 这样,将始终显示第一个值。 An easy way also could be a formula to do this dynamically. 一种简单的方法也可以是动态地执行此操作的公式。

R3: =P3
R4: =IF(COUNTIFS(P$3:P3,">"&P4/1.1,P$3:P3,"<"&P4*1.1),"",P4)

R4 can be copied down as you need it. R4可以根据需要向下复制。

Done by phone. 通过电话完成。 May contain errors. 可能包含错误。 ;) ;)

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

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