簡體   English   中英

VBA excel。 有條件循環

[英]VBA excel. Loop through with condition

我希望遍歷A列。
-如果下一個數字大於上一個數字,則繼續(A:0,1,2,3 ..)。
-這樣做直到下一個數字等於或小於(A:0,1,2,3,4,4 ..)。
-如果數字小於(A:0,1,2,3,4,3 ..)。 或等於,取最高的#4減去最低的#0,然后將結果放在columnB中最高編號旁邊。
-如果下一個數字等於上一個數字,則減去答案0並將其放在列B中。
-如果下一個數字小於上一個數字,請繼續。 這樣做直到下一個數字等於或小於。
-如果數字小於或等於,則取最高的#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
...  ...  

您可以使用字典...將行號添加到鍵值並檢查位置...

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM