Excel VBA确定向上或向下趋势

时间:2013-01-16 17:46:09

标签: vba excel-vba excel

我正在尝试创建一个程序,指定一组数字所处的趋势类型。向上趋势,向下趋势或无趋势。请参阅下面的excel样式表,以便您可以按照下一部分进行操作。

程序需要计算第3行。第3行要么具有U(对于Up)要么具有D(对于向下)或者没有。下面我解释如何指定一个U或者D这也就是我希望VBA程序为我做的。

让我们从C列开始。列C1的值为34.92,C2的值为+(34.92大于前一天的33.02)。现在我们转到第一个前面的“+”,其中至少有一个相反的符号(在本例中为“ - ”)。所以在这种情况下是A列(B列中的一个“ - ”)。现在,如果C1(34.92)中的数值大于A(33.12)中的数值,那么我们在C3中指定“U”。如果它不大,我们会在C3中留下一个空单元格。

让我们进入D列。列D1的值为35.19,大于C1值34.92,这就是D2具有“+”的原因。接下来我们转到第一个前面的“+”,其中至少有一个相反的符号(在本例中为“ - ”)。所以在这种情况下,再次是A列。由于D1(39.19)中的数值大于A1(33.12)中的数值,因此D3得到U.

转到F栏(32.97)。 32.97是LESS然后35.19(D1),这就是为什么F2是“ - ”。接下来我们转到第一个前面的“ - ”,其间至少有一个相反的符号(在本例中为“+”)。所以在这种情况下,这是B列(中间有两个“+”符号)。现在,因为我们正在处理“ - ”符号,这次我们查看F1中的数值是否为LESS,然后是B1中的数值...它是什么,因此在F3中输入“D”。如果F1大于B1,则单元格将为空。

到G列(35.21)。这大于32.97(F1),因此在G2中得到“+”。接下来我们转到第一个前面的“+”,其中至少有一个相反的符号(在这种情况下为“ - ”)。所以在这种情况下,这是D列(中间有一个“ - ”)。由于G1的数值大于D1的数值,我们指定“U”。如果不是更大,我们会将单元格留空。

Table 1: Initial Table before Program
        A        B        C        D        F        G        H        I
        Jan 1    Jan 2    jan 3    Jan 4    Jan 5    Jan 6    Jan 7    Jan 8
1       33.12    33.02    34.92    35.19    32.97    35.21    35.60    35.90
2       (+)      (-)      (+)      (+)      (-)      (+)      (+)      (+)
3                          ?        ?        ?        ?        ?        ?

Table 2: Final Table with Answers
        A        B        C        D        F        G        H        I
        Jan 1    Jan 2    jan 3    Jan 4    Jan 5    Jan 6    Jan 7    Jan 8
1       33.12    33.02    34.92    35.19    32.97    35.21    35.60    35.90
2       (+)      (-)      (+)      (+)      (-)      (+)      (+)      (+)
3                          U        U        D        U        U        U
Sub Comparison()

For Each Cell In Range("A3:I3")
currentSign = cell.Value
' Find out what the sign is in the cell before it
previousSign = Cell.Offset(0, -1).Value
'Variable used to find the first cell with an
'Opposite sign as the current cell
oppositeSign = Cell.Offset(0, -2).Value
'Variable to associate the numberical number above the first Opposite Sign Cell
oppositeNumericalCell = Cell.Offset(-1, -2).Value
' Create a Variable for Target Cell
Set targetSignCell = Cell.Offset(1, 0)
If currentSign.Value = "+" And currentSign.Value <> previousSign.Value And oppositeSign.Value = currentSign.Value And currentNumericalCell.Value > oppositeNumericalCell.Value Then
targetSignCell = "U"
ElseIf currentSign.Value = "-" And currentSign.Value <> previousSign.Value And oppositeSign.Value = currentSign.Value And currentNumericalCell.Value < oppositeNumericalCell.Value Then
targetSignCell = "D"
Else
End If
Next Cell
End Sub

1 个答案:

答案 0 :(得分:0)

经过轻微测试....

Sub tester()
    Dim c As Range
    'loop through the +/- range...
    For Each c In ActiveSheet.Range("A3:H3")
        CheckCell c
    Next c
End Sub

Sub CheckCell(c As Range)

    Dim pm As String, v As Double, v2 As Double
    Dim pm_opp As String, c2 As Range
    Dim oppFound As Boolean, pm_tmp As String


    v = c.Offset(-1, 0).Value        'value
    pm = c.Value       'sign
    pm_opp = IIf(pm = "+", "-", "+") 'opposite sign

    If c.Column = 1 Then Exit Sub

    Set c2 = c.Offset(0, -1)

    Do While c2.Value <> ""
        Debug.Print oppFound
        pm_tmp = c2.Value
        If Not oppFound Then
            oppFound = (pm_tmp = pm_opp)
        Else
            If pm_tmp = pm Then
                v2 = c2.Offset(-1, 0).Value
                If pm = "+" And (v2 < v) Then c.Offset(1, 0) = "U"
                If pm = "-" And (v2 > v) Then c.Offset(1, 0) = "D"
                Exit Do
            End If
        End If
        If c2.Column = 1 Then Exit Do
        Set c2 = c2.Offset(0, -1)
    Loop

End Sub
相关问题