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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值