VBA优秀。循环使用条件

时间:2015-10-19 22:51:15

标签: excel-vba loops vba excel

我希望循环通过A列。
  - 如果下一个数字大于前一个数字则继续(A:0,1,2,3 ..)   - 这样做直到下一个数字等于或小于(A:0,1,2,3,4,4 ..)   - 如果数字小于(A:0,1,2,3,4,3 ..)。或等于,取最高#4减去最低#0,并将结果放在最高数字旁边的columnB中   - 如果下一个数字等于前一个数字,则减去并将答案0放在columnB中   - 如果下一个数字低于前一个数字,则继续。这样做直到下一个数字等于或小于。
  - 如果数字小于或等于,则取最高#4减去最低#0 ...

我不确定如果我很清楚,但我认为循环可能适用于这种情况。或许任何其他想法将不胜感激。提前致谢。

     A   B
1    0   
2    1
3    2
4    3
5    4   4
6    4   0
7    3
8    2
9    1
10   0   4
11   1
12   2   2
13   2   0
14   3
15   4   2
...  ...  

1 个答案:

答案 0 :(得分:0)

您可以使用字典...将行号添加到键值并检查位置......

 Sub YourLoop()

   Dim dic As Scripting.Dictionary
   Set dic = New Scripting.Dictionary

   Dim i As Integer
   Dim n As Integer

For i = 1 To Rows.Count

     ''ColumnA values
     dic.Add i, Cells(i, 1).Value

Next i


Dim k1 As Integer
Dim k2 As Integer
Dim k3 As Integer
Dim k4 As Integer

Dim v1 As Integer
Dim v2 As Integer
Dim v3 As Integer
Dim v4 As Integer
Dim v As Integer

Dim c As Integer
c = 1

For Each key In dic.Keys

   v = dic(key)

   If c = 1 Then
    ''do nothing
   ElseIf c = 2 Then
    k1 = key - 1
    v1 = dic(k1)

    If v <= v1 Then

    End If

   ElseIf c = 3 Then
    k2 = key - 2
    k1 = key - 1

    v1 = dic(k1)
    v2 = dic(k2)


   ElseIf c >= 4 And c < dic.Count Then

    k4 = key - 4
    k3 = key - 3
    k2 = key - 2
    k1 = key - 1

    v1 = dic(k1)
    v2 = dic(k2)
    v3 = dic(k3)
    v4 = dic(k4)

    ElseIf c = dic.Count Then

    End If

    c = c + 1
Next
相关问题