带有动态边界的求和循环中的错误1004(也...慢)

时间:2016-08-09 10:02:22

标签: vba sum

我是VBA的新手,坐着一笔钱,其中包括三个if循环。代码看起来像这样

Dim strKonto As String
Dim str?r As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer


For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
    For k = 1 To 20 '200
        yearkbottom = (wsRefor.Cells(k + 3, 1) - 2007) + 1
        yearktop = (yearkbottom - 2007) + 4000

        For j = yearkbottom To yearktop

            strKonto = Right(wsArk7.Cells(j + 4, 2), 4)
            str?r = wsArk7.Cells(j + 4, 1)
            strUdbetaling = Left(wsArk7.Cells(j + 4, 2), 1)
            counter = Val(str?r) - 2007

            If wsRefor.Cells(i + 1, 2) = strKonto Then
                If wsRefor.Cells(1, k + 3) = str?r Then
                    If strUdbetaling = 2 Then
                        wsRefor.Cells(i + 1, k + 3) = wsRefor.Cells(i + 1, k + 3) + wsArk7.Cells(j + 4, k + 2 - counter * 12)
                    End If
                End If
            End If
        Next j
    Next k
Next i

对于j循环,我试图使边界动态化,使计算速度稍慢。也就是说,我确信j循环找到的所有值都不会分布在j的整个范围内,而是使用k在上面定义的范围内。

但是,当我进行此更改时,我得到1004“应用程序定义或对象定义的错误”。

任何人都能发现错误,或者建议任何加速总和的方法吗?

最佳,

ID

编辑:我发现了问题所在。 j计数器在新边界的某个点上呈现零,当发生这种情况时(或者当它发生负数时),我得到的错误就出现了。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试以下一项。我试过做一些微调。

Dim strKonto As String
Dim strr As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer

Dim arryearkbottom(20) 'Declaring a Static Array of Size 21,(Starts from 0 index)
Dim arryearktop(20) 'Declaring a Static Array of Size 21,(Starts from 0 index)

'Initial Values
screenUpdateState = Application.ScreenUpdating

statusBarState = Application.DisplayStatusBar

calcState = Application.Calculation

eventsState = Application.EnableEvents

'Turning off the values for performance
Application.ScreenUpdating = False

Application.DisplayStatusBar = False

Application.Calculation = xlCalculationManual

Application.EnableEvents = False

'Reading all the values from sheet to Arrays
For i = 1 To 20
    arryearkbottom(i) = (wsRefor.Cells(i + 3, 1) - 2007) + 1
    arryearktop(i) = (arryearkbottom(i) - 2007) + 4000

Next


For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
    For k = 1 To 20 '200
        'yearkbottom = (wsRefor.Cells(k + 3, 1) - 2007) + 1
        'yearktop = (yearkbottom - 2007) + 4000
         For j = arryearkbottom(k) To arryearktop(k)

            strKonto = Right(wsArk7.Cells(j + 4, 2), 4)
            strr = wsArk7.Cells(j + 4, 1)
            strUdbetaling = Left(wsArk7.Cells(j + 4, 2), 1)
            counter = Val(strr) - 2007

            If wsRefor.Cells(i + 1, 2) = strKonto Then
                If wsRefor.Cells(1, k + 3) = strr Then
                    If strUdbetaling = 2 Then
                        wsRefor.Cells(i + 1, k + 3) = wsRefor.Cells(i + 1, k + 3) + wsArk7.Cells(j + 4, k + 2 - counter * 12)
                    End If
                End If
            End If
        Next j
    Next k
Next i


'Setting to Initial values
Application.ScreenUpdating = screenUpdateState

Application.DisplayStatusBar = statusBarState

Application.Calculation = calcState

Application.EnableEvents = eventsState

溶液2:

Dim strKonto As String
Dim strr As String
Dim strUdbetaling As String
Dim counter As Integer
Dim yearkbottom As Integer
Dim yearktop As Integer
Dim rngwsRefor As Range
Dim rngwsArk7 As Range

'Initial Values
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents

'Turning off the values for performance
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

'Reading Entire sheet values to range
Set rngwsRefor = wsRefor.Cells
Set rngwsArk7 = wsArk7.Cells


For i = 1 To 50 'wsRefor.Cells(Rows.Count, 2).End(xlUp).Row
    For k = 1 To 20 '200
        yearkbottom = (rngwsRefor(k + 3, 1).Value - 2007) + 1
        yearktop = (yearkbottom - 2007) + 4000
         For j = yearkbottom To yearktop

            strKonto = Right(rngwsArk7(j + 4, 2).Value, 4)
            strr = rngwsArk7(j + 4, 1).Value
            strUdbetaling = Left(rngwsArk7(j + 4, 2).Value, 1)
            counter = Val(strr) - 2007

            If rngwsRefor(i + 1, 2).Value = strKonto Then
                If rngwsRefor(1, k + 3).Value = strr Then
                    If strUdbetaling = 2 Then
                        rngwsRefor(i + 1, k + 3).Value = rngwsRefor(i + 1, k + 3).Value + rngwsArk7.Cells(j + 4, k + 2 - counter * 12).Value
                    End If
                End If
            End If
        Next j
    Next k
Next i


'Setting to Initial values
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState