getting the maximum value calculated from 2 columns of data (without helper column)

心不动则不痛 提交于 2020-07-10 07:59:15

问题


I have some data, stored in Column A and column B. For each row, I would like to get the difference between the values in column A and column B. I.e. cells(1,2) - cells(1,1), so on so forth for the rest of rows. The second step is to determine if the difference on i row was the max of the preceeding nth rows. With helper column, I can do it with formula or with code like this.

for i = 1 to LR
cells(i,3) = cells(i,2) - cells(i,1)
next i

for i = 1 to LR
if cells(i,3) = .max(range(cells(i-19,3),cells(i,3))) then
cells(i,4) = "MAX"
end if
next i

While I don't want to add a helper column, what vba codes can do this? Can you show me the code for I may not be familiar with the syntax I needed?


回答1:


This does what your formula does, without using the helper column. I'm assuming the end goal is to get the word "MAX" written next to the set where the difference peaks

Sub PeakDifferences()

Dim i As Long
Dim j As Long
Dim myArr() As Double
Dim subArr(1 To 20) As Double 'change 20 to 19 here if only looking at last 19 numbers

    With ActiveSheet

        lr = .Cells(.Rows.Count, "A").End(xlUp).Row
        ReDim myArr(1 To lr)

        For i = 1 To lr
            myArr(i) = .Cells(i, 2).value - .Cells(i, 1).value
            For j = LBound(subArr) To UBound(subArr)
                If i - j < 0 Then Exit For
                subArr(j) = myArr(i - j + 1)
            Next
            If myArr(i) = WorksheetFunction.Max(subArr) Then .Cells(i, 3) = "MAX"
        Next i

    End With

End Sub



回答2:


Does this do what you need?

Sub e()
Dim i As Long
Dim m As Double
For i = 1 To LR
    If Abs(Cells(i, 1) - Cells(i, 2)) > m Then m = Abs(Cells(i, 1) - Cells(i, 2))
Next i
MsgBox m
End Sub

Cycles through the rows and checks if the current maximum difference m is outnumbered by the current row difference. It's wrapped in Abs to help with any negative values

BUT as pointed out by JvdV, this isn't the same thing - my answer is looking for biggest difference, rather than maximum value;

-150 is a bigger difference than 100, but 100 is the max val between -150 and 100.




回答3:


Quick and dirty:

Sub Test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lr As Long, mx As Double

With ws
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    mx = .Evaluate(Replace("MAX(B1:BY-A1:AY)", "Y", lr))
End With

End Sub

Reading comment, you want to return "MAX" to column C where the result of that particular row is been the max value up to that point. Again, quick and dirty:

Sub Test2()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lr As Long, mx As Double

With ws
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    .Range("C1").FormulaArray = "=IF(INDEX(B:B,ROW())-INDEX(A:A,ROW())=MAX(B$1:INDEX(B:B,ROW())-A$1:INDEX(A:A,ROW())),""MAX"","""")"
    .Range("C1:C" & lr).FillDown
End With

End Sub

A possibly better way would be to loop arrays, but other answers cover this approach. I'll stick to Evaluate in this example =)



来源:https://stackoverflow.com/questions/62208560/getting-the-maximum-value-calculated-from-2-columns-of-data-without-helper-colu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!