IF语句没有输出正确的值

时间:2016-02-25 18:57:08

标签: excel vba

所以我一直在尝试使用vba自动化excel中的进程,我似乎遇到了麻烦。我以前没有做过很多vba,我很感激任何帮助。我之前问过几个问题,并在谷歌和这里查询信息,但我的程序仍然无法正常运行。

如果我只运行一次这段代码,那么它的工作原理非常好,但我想运行它30次,结果不断更新并将新答案放入不同的单元格然后它不起作用。基本上会为第一次运行返回正确的答案,但每次都会产生错误的响应。

以下是我的代码,对此的任何帮助都将非常感谢!!!

For i = 19 To 19

    Range("AA3:AA7").Select
    Selection.ClearContents

    If Cells(i, 26) > 499999 Then
        Cells(3, 27) = 499999
    ElseIf Cells(i, 26) < 499999 Then
        Cells(3, 27) = Cells(i, 26)
    End If

    If Cells(3, 27) < 499999 Then
         Cells(5, 27) = 0
    ElseIf Cells(i, 26) > 999999 Then
        Cells(4, 27) = 500000
    ElseIf Cells(i, 26) < 999999 Then
        Cells(4, 27) = Cells(i, 26) - 499999
    End If

    If Cells(4, 27) = 0 Then
         Cells(5, 27) = 0
    ElseIf Cells(i, 26) > 1999999 Then
        Cells(5, 27) = 1000000
    ElseIf Cells(i, 26) < 1999999 Then
        Cells(5, 27) = Cells(i, 26) - 999999
    End If

    If Cells(5, 27) = 0 Then
         Cells(6, 27) = 0
    ElseIf Cells(i, 26) > 4999999 Then
         Cells(6, 27) = 3000000
    ElseIf Cells(i, 26) < 4999999 Then
        Cells(6, 27) = Cells(i, 26) - 1999999
    End If

    If Cells(6, 27) = 0 Then
         Cells(7, 27) = 0
    ElseIf Cells(6, 27) = 3000000 Then
         Cells(7, 27) = Cells(3, i) - 4999999
    End If

    Cells(i, 30).Value = (Cells(3, 28) + Cells(4, 28) + Cells(5, 28) + Cells(6, 28) + Cells(7, 28)) / Cells(i, 26)

 Next i

1 个答案:

答案 0 :(得分:0)

当值恰好是数字时,你还没有任何东西,例如&lt; 499999和&gt; 499999没有考虑= 499999。

你也不需要陈述下一个 - 只是下一个会这样做。

我不确定为什么要更新单元格然后将它们一起添加,最好将单元格值分配给变量,如下所示。如果您想查看它出错的地方,我可能会想要添加msgbox调用以在迭代时显示值:

  

Dim I as integer

Dim value1,value2,value3,value4,value5 as integer

对于i = 19至49

If Cells(i, 26) > 499999 Then
    Value1=499999
Else 
     Value1=Cells(i, 26)
End If

'依旧......

Cells(i, 30).Value = value1+value2 etc...

下一步

相关问题