If语句中的VBA运行时错误1004

时间:2017-09-25 14:33:55

标签: excel vba excel-vba runtime-error

大家好日子, 执行代码时,我一直遇到相同的运行时错误。我没有接受过VBA的正式培训(主要是高中的VB)。 代码就是这个

Sub Lavaggi2():
Dim i, j, k, lavaggio, x, daymax As Integer
Dim day As Date
Dim Ore(10) As Single
Dim column_len, row_len As Integer
Dim totale_ore As Integer

'Determining variable for row and columns
column_len = Sheets("Foglio7").Cells.CurrentRegion.Columns.Count
row_len = Sheets("Foglio7").Cells.CurrentRegion.Rows.Count
k = 1

For j = 1 To row_len
    For i = 1 To column_len
        If (Sheets("Foglio7").Cells(2, i).Value = "Codice") Then
            If (Sheets("Foglio7").Cells(j, i).Value = "00/100" Or Sheets("Foglio7").Cells(j, i).Value = "00/200") Then
                day = Sheets("Foglio7").Cells(j, 1).Value
                    For k = 1 To 10
                        If (Sheets("Foglio7").Cells(j - k, 1).Value = day) Then
                            Ore(k) = Sheets("Foglio7").Cells(j - k, i + 5).Value
                            daymax = daymax + 1
                            Else
                        End If
                    Next k
                totale_ore = Worksheet.funcion.Sum(Ore)
                lavaggio = Sheets("Foglio7").Cells(j, i + 7) / totale_ore
                    For x = 1 To daymax
                        Sheets("Foglio7").Cells(j - x, i + 7).Value = lavaggio * Ore(x)
                    Next x
                Erase Ore
            End If
        End If
    Next i
Next j

End Sub

我收到错误的行是

   If (Sheets("Foglio7").Cells(j - k, 1).Value = day) Then

我很确定这是傻事,但我无法绕过它。

PS:我知道代码可能有点笨重,但我会在未来阶段简化它。

感谢所有回答的人

2 个答案:

答案 0 :(得分:2)

在循环的第一次迭代中,j - k将等于0,而您的单元格将为.Cells(0, 1),这不存在。

答案 1 :(得分:0)

我设法解决了我遇到的问题。它按预期工作。感谢大家的帮助

Sub Lavaggi2():
Dim i, j, k, x, daymax As Integer
Dim day As Date
Dim lavaggio, totale_ore, Ore(10) As Double
Dim column_len, row_len As Integer


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

column_len = Sheets("Foglio7").Cells.CurrentRegion.Columns.Count
row_len = Sheets("Foglio7").Cells.CurrentRegion.Rows.Count

daymax = 1
For j = 1 To row_len
    For i = 1 To column_len
        If (Sheets("Foglio7").Cells(2, i).Value = "Codice") Then
            If (Sheets("Foglio7").Cells(j, i).Value = "00/100" Or Sheets("Foglio7").Cells(j, i).Value = "00/200") Then
                day = Sheets("Foglio7").Cells(j, 1).Value
                    For k = 1 To 10
                        If (Sheets("Foglio7").Cells(j - k, 1).Value = day) Then
                            Ore(k) = Sheets("Foglio7").Cells(j - k, i + 5).Value
                            daymax = daymax + 1
                            Else
                            Exit For
                        End If
                    Next k
                totale_ore = Application.WorksheetFunction.Sum(Ore)
                lavaggio = Sheets("Foglio7").Cells(j, i + 7) / totale_ore
                    For x = 1 To daymax - 1
                        Sheets("Foglio7").Cells(j - x, i + 7).Value = lavaggio * Ore(x)
                    Next x
                daymax = 1
                Erase Ore
            End If
        End If
    Next i
Next j

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

我还调整了声明,以便在最终结果中达到所需的精度。