Excel VBA的循環與判斷

以下代碼們都是有業務邏輯的,展示在這裏,只是想讓自己別忘了一些寫法,至於業務邏輯,
各位想必沒有興趣,有興趣的可以學習

Sub «ö1_Click()
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim biao_zhi As Boolean
Dim yong_liang As Double
n = 2
For i = 3 To 10000
    If Len(Sheet1.Cells(i, 16).Value) < 2 Then
       Exit For
    End If
    yong_liang = 0
    biao_zhi = True
    For m = 2 To 10000
        If Len(Sheet3.Cells(m, 1).Value) < 2 Then
           Exit For
        End If
        If Sheet3.Cells(m, 2) = Sheet1.Cells(i, 2).Value _
        And Sheet3.Cells(m, 1).Value = Sheet1.Cells(i, 16).Value Then
            biao_zhi = False
            Exit For
        End If
    Next m
    If biao_zhi = True Then
       For j = 3 To 10000
          If Len(Sheet1.Cells(j, 16).Value) < 2 Then
             Exit For
          End If
          If Sheet1.Cells(j, 2).Value = Sheet1.Cells(i, 2).Value _
              And Sheet1.Cells(j, 16).Value = Sheet1.Cells(i, 16).Value Then
                yong_liang = yong_liang + Sheet1.Cells(j, 9).Value
          End If
        Next j
          Sheet3.Cells(n, 1).Value = Sheet1.Cells(i, 16).Value
          Sheet3.Cells(n, 2).Value = Sheet1.Cells(i, 2).Value
          Sheet3.Cells(n, 3).Value = yong_liang
          n = n + 1
     End If
Next i
End Sub
Sub «ö2_Click()
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim n As Integer
Dim biao_zhi As Boolean
Dim yong_liang As Double
n = 2
For i = 3 To 10000
    If Len(Sheet2.Cells(i, 16).Value) < 2 Then
       Exit For
    End If
    yong_liang = 0
    biao_zhi = True
    For m = 2 To 10000
        If Len(Sheet3.Cells(7, 1).Value) < 2 Then
           Exit For
        End If
        If Sheet3.Cells(m, 8) = Sheet2.Cells(i, 2).Value _
        And Sheet3.Cells(m, 7).Value = Sheet2.Cells(i, 16).Value Then
            biao_zhi = False
            Exit For
        End If
    Next m
    If biao_zhi = True Then
       For j = 3 To 10000
          If Len(Sheet2.Cells(j, 16).Value) < 2 Then
             Exit For
          End If
          If Sheet2.Cells(j, 2).Value = Sheet2.Cells(i, 2).Value _
              And Sheet2.Cells(j, 16).Value = Sheet2.Cells(i, 16).Value Then
                yong_liang = yong_liang + Sheet2.Cells(j, 9).Value
          End If
        Next j
          Sheet3.Cells(n, 7).Value = Sheet2.Cells(i, 16).Value
          Sheet3.Cells(n, 8).Value = Sheet2.Cells(i, 2).Value
          Sheet3.Cells(n, 9).Value = yong_liang
          n = n + 1
     End If
Next i
End Sub
Sub «ö3_Click()
'Selection.Value = "Test1"
Dim i As Integer
Dim j As Integer
Dim n As Integer
n = 2
For i = 2 To 10000
    If Len(Sheet3.Cells(i, 1).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 2 To 10000
        If Len(Sheet3.Cells(j, 1).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 1).Value = Sheet3.Cells(j, 7).Value _
        And Sheet3.Cells(i, 2).Value = Sheet3.Cells(j, 8).Value _
        And Sheet3.Cells(j, 9).Value - Sheet3.Cells(i, 3).Value <> 0 Then
            Sheet3.Cells(n, 12).Value = Sheet3.Cells(i, 1).Value
            Sheet3.Cells(n, 13).Value = Sheet3.Cells(i, 2).Value
            Sheet3.Cells(n, 14).Value = Sheet3.Cells(i, 3).Value
            Sheet3.Cells(n, 15).Value = Sheet3.Cells(j, 9).Value
            n = n + 1
        End If
    Next j
Next i
End Sub
Sub «ö4_Click()
'Selection.Value = "Test2"
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim a As Integer
n = 2
For i = 3 To 10000
    a = 0
    If Len(Sheet3.Cells(i, 1).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 3 To 10000
        If Len(Sheet3.Cells(j, 7).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 1).Value = Sheet3.Cells(j, 7).Value _
        And Sheet3.Cells(i, 2).Value = Sheet3.Cells(j, 8).Value Then
            a = 1
        End If
    Next j
    If a = 0 Then
       Sheet3.Cells(n, 18).Value = Sheet3.Cells(i, 1).Value
       Sheet3.Cells(n, 19).Value = Sheet3.Cells(i, 2).Value
       Sheet3.Cells(n, 20).Value = Sheet3.Cells(i, 3).Value
       'Selection.Value = i
       n = n + 1
    End If
Next i
End Sub
Sub «ö5_Click()
'Selection.Value = "Test2"
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim a As Integer
n = 2
For i = 3 To 10000
    a = 0
    If Len(Sheet3.Cells(i, 7).Value) < 2 Then
        Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
    End If
    For j = 3 To 10000
        If Len(Sheet3.Cells(j, 1).Value) < 2 Then
             Exit For '¦pªG²£«~®Æ¸¹¬°ªÅªº®É­Ô¡A´N°h¥X´`Àô
        End If
        If Sheet3.Cells(i, 7).Value = Sheet3.Cells(j, 1).Value _
        And Sheet3.Cells(i, 8).Value = Sheet3.Cells(j, 2).Value Then
            a = 1
        End If
    Next j
    If a = 0 Then
       Sheet3.Cells(n, 23).Value = Sheet3.Cells(i, 7).Value
       Sheet3.Cells(n, 24).Value = Sheet3.Cells(i, 8).Value
       Sheet3.Cells(n, 25).Value = Sheet3.Cells(i, 9).Value
       'Selection.Value = i
       n = n + 1
    End If
Next i
End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章