查找值的行位置并将其用于VBA中的计算

时间:2017-01-25 08:56:25

标签: excel vba excel-vba

我有一个定义范围的代码,并找到该范围的最高和最低值。

我想要做的是获取这些值,找到每个值的行,看看高值的行是否小于低值的行,然后进行计算。

问题1 由于某种原因,我在使用行进行比较的行中不断收到无效的限定符错误。

问题2 如果低值出现在高位之前,我怎样才能找到低位之前的最高值?

代码的相关部分是:

 Set MDDRange = Worksheets("TIME").Range(Col_Letter(lColumn) & 2, Col_Letter(lColumn) & LastRow) '.Value

        MDDHigh = Application.WorksheetFunction.Min(MDDRange)
        MDDHighPos = Worksheets("TIME SERIES").Range(MDDRange).Find(MDDHigh, LookIn:=xlWhole)

        MDDLow = Application.WorksheetFunction.Max(MDDRange)
        MDDLowPos = Worksheets("TIME").Range(MDDRange).Find(MDDLow, LookIn:=xlWhole)


'if to see if mddhigh comes before mddlow

        If MDDHighPos.Row + 1 < MDDLowPos.Row + 1 Then ' error is here

            MDD = (MDDLow - MDDHigh) / MDDHigh

            Worksheets("STATS").Cells(6, lColumn).Value = MDD

        Else
            'if my high value is not before my low, how to find the highest value before the low?
        End If

任何帮助都将深受赞赏。

修正部分纠正这两个问题(这来自SJR接受的答案和R3uK的修改):

Set MDDRange = Worksheets("TIME").Range(Col_Letter(lColumn) & 2, Col_Letter(lColumn) & LastRow) '.Value

        MDDHigh = Application.WorksheetFunction.Max(MDDRange)
        Set MDDHighPos = MDDRange.Find(MDDHigh)

        MDDLow = Application.WorksheetFunction.Min(MDDRange)
        Set MDDLowPos = MDDRange.Find(MDDLow)

        a = MDDHighPos.Row
        b = MDDLowPos.Row

'if to see if mddhigh comes before mddlow

        If a < b Then

            MDD = ((MDDLow - MDDHigh) / MDDHigh)

            Worksheets("STATS").Cells(6, lColumn).Value = MDD

        Else
'this creates a new range from begin of old range to the lowest value, and finds the highest value in this range, then calculates the drawdown

            Set MDDRangeNew = Worksheets("TIME").Range(Col_Letter(lColumn) & 2, Col_Letter(lColumn) & b)

            MDDHighNew = Application.WorksheetFunction.Max(MDDRangeNew)

            MDD = ((MDDLow - MDDHighNew) / MDDHighNew)

            Worksheets("STATS").Cells(6, lColumn).Value = MDD

        End If

1 个答案:

答案 0 :(得分:2)

你走了。我假设您的变量声明在开头......

Set MDDRange = Worksheets("TIME").Range(Col_Letter(lColumn) & 2, Col_Letter(lColumn) & LastRow) '.Value

        MDDHigh = Application.WorksheetFunction.Min(MDDRange)
        Set MDDHighPos = Worksheets("TIME SERIES").Range(MDDRange).Find(MDDHigh, LookIn:=xlWhole)

        MDDLow = Application.WorksheetFunction.Max(MDDRange)
        Set MDDLowPos = Worksheets("TIME").Range(MDDRange).Find(MDDLow, LookIn:=xlWhole)


'if to see if mddhigh comes before mddlow

        If MDDHighPos.Row + 1 < MDDLowPos.Row + 1 Then ' error is here

            MDD = (MDDLow - MDDHigh) / MDDHigh

            Worksheets("STATS").Cells(6, lColumn).Value = MDD

        Else
            'if my high value is not before my low, how to find the highest value before the low?

             Set MDDRange = Worksheets("TIME").Range(Col_Letter(lColumn) & "2", Col_Letter(lColumn) & MDDLowPos.Row - 1)
             'And here you go again! ;)

        End If