溢流故障6 VBA While-loop

时间:2018-01-24 23:58:07

标签: vba excel-vba while-loop overflow excel

首先感谢帮助我。 我编写了一个Sub,我总是得到一个溢出故障6.它在While x<160的行中。我不能做到160以上。例如如果我做了170我会得到错误。但我想do While x<1000

目标是总结持续时间。例如。在2017年1月半,由于电气原因我们有2个停机时间,第一个是2分钟,第二个是10分钟。

这个宏正在对1/2/17进行总结,说因为电气原因,我们有12分钟的停机时间。

我不是以专业的方式做到这一点,但这会让我的工作变得更加容易!它的工作。如果我想写大量的话,我只会遇到这种烦人的溢出故障。

Sub sumuptheduration()


    'Then sum up all the duration times

   Dim a As Integer
   Dim b As Integer
   Dim c As Integer
   Dim x As Long
   Dim summe As Variant
   Dim valuecells As Range


   a = 1
   b = 2
   c = 0
   x = 1

   Set valuecells = Worksheets(1).Range("P2")


   Do While Worksheets(1).Range("C" & b).Value = Worksheets(1).Range("C" & b+a).Value And Worksheets(1).Range("J" & b).Value = Worksheets(1).Range("J" & b + a).Value

        Set valuecells = Worksheets(1).Range("P" & b & ":P" & a + b)
        a = a + 1


    Loop


    Worksheets(1).Range("S" & a + (b - 1)).Value = Application.WorksheetFunction.Sum(valuecells)




    b = a + 2
    c = a + 2
    a = 1




    Dim y As Integer
    y = 0






   While x < 160


    Do While Worksheets(1).Range("C" & b).Value = Worksheets(1).Range("C" & b + a).Value And Worksheets(1).Range("J" & b).Value = Worksheets(1).Range("J" & b + a).Value

        Set valuecells = Worksheets(1).Range("P" & b & ":P" & c + a)

        a = a + 1

    Loop





    Worksheets(1).Range("S" & c + (a - 1)).Value = Application.WorksheetFunction.Sum(valuecells) 'Writes the Sum into the correct Cell (last one of combination)


        'This if clause checks if there are "Single combinations" because single combinations do not go through the do while at the top


        If Worksheets(1).Range("J" & b).Value <> Worksheets(1).Range("J" & b + a).Value And Worksheets(1).Range("J" & b).Value <> Worksheets(1).Range("J" & b + (a - 2)).Value Or Worksheets(1).Range("C" & b).Value <> Worksheets(1).Range("C" & b + a).Value And Worksheets(1).Range("C" & b).Value <> Worksheets(1).Range("C" & b + (a - 2)).Value Then


            Worksheets(1).Range("S" & b).Value = Worksheets(1).Range("P" & b).Value

        Else

        End If


    'Change the variables into correct way for the next passing of the do while clause
    x = x + 1 'causes that do while is not just one time passes
    b = c + a
    c = c + a 'temporary variable to not forget the old b during the next passing of the do while
    a = 1 'at the end of every passing a tells you how much times you needed to pass the do while, so you have to reset it every time



    Wend

      End Sub





here

1 个答案:

答案 0 :(得分:2)

更改声明如下:

 Dim a As Long
 Dim b As Long
 Dim c As Long

不要将这些变量声明为Integer