如果循环在VBA中不起作用

时间:2017-09-20 06:43:55

标签: vba excel-vba excel

任何人都可以帮助我理解为什么下面粘贴的VBA中的代码不起作用吗?

我是VBA的新手,部分代码可能看起来很原始 该代码应该给出2位数字并将其粘贴到某个位置,但它会丢弃3位数字和随机位置。

Dim M As Integer
'M = 24

For i = 7 To 19 And M = 24 Step 1
    If M = 36 Then
        Exit For
    End If

    If ActiveSheet.Cells(7, i) >= 0 And ActiveSheet.Cells(7, i) <= 15 And ActiveSheet.Cells(8, i) <= 15 Then
        ActiveSheet.Cells(M, 6) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
    End If

    If ActiveSheet.Cells(7, i) >= 345 And ActiveSheet.Cells(8, i) <= 360 Or ActiveSheet.Cells(7, i) >= 345 And ActiveSheet.Cells(8, i) <= 0 Then
        ActiveSheet.Cells(M, 6) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 6) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 7) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 7) Then
            ActiveSheet.Cells(M, 6) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 6) = (ActiveSheet.Cells(23, 7) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 7) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 7)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 8) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 9) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 9) Then
            ActiveSheet.Cells(M, 7) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 7) = (ActiveSheet.Cells(23, 9) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 8) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 9)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 10) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 11) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 11) Then
            ActiveSheet.Cells(M, 8) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) * 0.3
        Else
            ActiveSheet.Cells(M, 8) = (ActiveSheet.Cells(23, 11) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 9) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 11)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 12) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 13) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 13) Then
            ActiveSheet.Cells(M, 9) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 9) = (ActiveSheet.Cells(23, 13) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 10) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 13)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 14) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 15) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 15) Then
            ActiveSheet.Cells(M, 10) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 10) = (ActiveSheet.Cells(23, 15) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 11) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 15)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 16) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 17) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 17) Then
            ActiveSheet.Cells(M, 11) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 11) = (ActiveSheet.Cells(23, 17) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 12) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 17)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 18) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 19) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 19) Then
            ActiveSheet.Cells(M, 12) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 12) = (ActiveSheet.Cells(23, 19) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 13) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 19)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 20) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 21) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 21) Then
            ActiveSheet.Cells(M, 13) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 13) = (ActiveSheet.Cells(23, 21) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 14) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 21)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 22) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 23) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 23) Then
            ActiveSheet.Cells(M, 14) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
             ActiveSheet.Cells(M, 14) = (ActiveSheet.Cells(23, 23) - ActiveSheet.Cells(7, i)) / 30 * 100
              ActiveSheet.Cells(M, 15) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 23)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 24) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 25) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 25) Then
            ActiveSheet.Cells(M, 15) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 15) = (ActiveSheet.Cells(23, 25) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 16) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 25)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 26) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 27) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 27) Then
            ActiveSheet.Cells(M, 16) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 16) = (ActiveSheet.Cells(23, 25) - ActiveSheet.Cells(7, i)) / 30 * 100
            ActiveSheet.Cells(M, 17) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 25)) / 30 * 100
        End If
    End If

    If ActiveSheet.Cells(7, i) >= ActiveSheet.Cells(23, 28) And ActiveSheet.Cells(7, i) <= ActiveSheet.Cells(23, 29) Then
        If ActiveSheet.Cells(8, i) <= ActiveSheet.Cells(23, 29) Then
            ActiveSheet.Cells(M, 17) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(7, i)) / 30 * 100
        Else
            ActiveSheet.Cells(M, 17) = (ActiveSheet.Cells(23, 29) - ActiveSheet.Cells(7, i)) / 30 * 100
             ActiveSheet.Cells(M, 6) = (ActiveSheet.Cells(8, i) - ActiveSheet.Cells(23, 29)) / 30 * 100
        End If
    End If

    M = M + 1
Next i

1 个答案:

答案 0 :(得分:0)

M = 24之前提取For可能会帮助您获得理想的结果:

M = 24
For i = 7 To 19 
....