Excel VBA由EndRow引起的应用程序定义或对象定义的错误

时间:2015-02-01 05:44:50

标签: excel vba excel-vba

我有一个Excel Workbook,其中包含最初在单独运行时完美运行的宏...但是,现在我将它们链接在一起,它会出错:应用程序定义或对象定义的错误。在调试时,它将我带到Excel所述的一行导致问题,但所有涉及的对象都存在。有谁可以看看并提供任何建议或帮助?

非常感谢你!

编辑:如果你无法访问Dropbox,这里是脚本:

Dim U As Worksheet
Dim S As Worksheet
Sub Forecast()
Set U = Worksheets("Usage_Data")
Sheets.Add.Name = "Project Setup"
Set S = Worksheets("Project Setup")
'This part of the Macro simply enters and formats the data shown below.
    S.Range("A1") = "Usage Data"
    S.Range("A2") = "DOS"
    S.Range("A3") = "Periods"
    S.Range("A4") = "Rows"
    S.Range("B1") = "730"
    S.Range("B2") = "30"
    S.Range("B3") = "=R[-2]C/R[-1]C"
    S.Range("B4") = "=ROUNDUP(R[-1]C,)"
    S.Columns("A:A").HorizontalAlignment = xlCenter
    S.Columns("A:A").VerticalAlignment = xlBottom
    S.Columns("A:A").Font.Bold = True
    S.Columns("A:A").EntireColumn.AutoFit
    S.Columns("B:B").HorizontalAlignment = xlRight
    S.Columns("B:B").VerticalAlignment = xlBottom


'This part of the macro will calculate the dates for the time periods.
   Dim ud As Integer, dos As Integer, r As Integer, i As Integer

ud = S.Range("B1")
dos = S.Range("B2")
r = S.Range("B4")
For i = 1 To r
 Cells(i, "D") = Date - ud + (i - 1) * dos
Next i

'This part of the Macro will put the Period number beside the ending date of the period.
For i = 1 To r - 1
    S.Range("E1").Offset(i - 1, 0) = "Period " & i
Next i

'This part of the macro will properly format the date of the demand so that it can be used in the lookup formula below.
    LastRow = U.Range("F" & Rows.Count).End(xlUp).Row
    'Change LastRow for final data***************************
    U.Columns("F:F").Insert Shift:=xlToRight
    U.Range("F1") = "PERIOD"
    U.Range("F2") = "=VLOOKUP(E2,'Project Setup'!$D:$E,2,1)"
    U.Range("F2").Copy U.Range("F2:f" & LastRow)
    U.Range("f2:F" & LastRow).Copy
    U.Range("F2:F" & LastRow).PasteSpecial xlPasteValues
    'Call PT
    'Call Stats  REMOVE THIS ONE
    Call fill
   MsgBox "Setup Complete"

End Sub

这里是fill()

Sub fill()

Set r1 = Sheets("Sheet4")
Dim ad As Variant
Dim ad2 As Variant
Dim start As Variant
Dim endrow As Long
Dim endrow2 As Long
Dim c2 As Range
Set r2 = Sheets("Project Setup")
Set r3 = Sheets("Usage_Data")
Dim m As Long

ad = r2.Range("E:E").End(xlDown).Address
endrow = Range(ad).Row

'r1.Cells(1, 4).Value = endrow

'For each item in Usage data Col M, make Period Data. Col M has all the unique Item values
For j = 2 To 10

ad2 = r1.Range("A1").End(xlDown).Address
endrow2 = Range(ad2).Row
'r1.Cells(1, 5).Value = endrow2

    For i = 1 To endrow
    r1.Cells(endrow2 + i, 2).Value = r2.Cells(i, 5).Value ' Period*********************************
    r1.Cells(endrow2 + i, 1).Value = r3.Cells(j, 13).Value 'Item Number
    r1.Cells(endrow2 + i, 3).Value = Application.SumIfs(r3.Range("I:I"), r3.Range("H:H"), r3.Cells(j, 13).Value, r3.Range("F:F"), r2.Cells(i, 5).Value)
    Next

ad2 = r1.Range("A1").End(xlDown).Address
endrow2 = Range(ad2).Row

r1.Cells(endrow2 + 1, 1).Value = "FORECAST"
r1.Cells(endrow2 + 2, 1).Value = "MEAN"
r1.Cells(endrow2 + 2, 3).Value = Application.Average(r1.Range("C" & (endrow2 - 23) & ":" & "C" & endrow2))
r1.Cells(endrow2 + 3, 1).Value = "R.O.P"

'Exponential Smoothing Data for Col D:F
For i = 23 To 0 Step -1
r1.Cells(endrow2 + 1 - i, 4).Value = Application.WorksheetFunction.RoundDown(Cells(endrow2 - i, 4).Value + 0.2 * (Cells(endrow2 - i, 3).Value - Cells(endrow2 - i, 4).Value), 0)
r1.Cells(endrow2 + 1 - i, 5).Value = Application.WorksheetFunction.RoundDown(Cells(endrow2 - i, 5).Value + 0.5 * (Cells(endrow2 - i, 3).Value - Cells(endrow2 - i, 5).Value), 0)
r1.Cells(endrow2 + 1 - i, 6).Value = Application.WorksheetFunction.RoundDown(Cells(endrow2 - i, 6).Value + 0.8 * (Cells(endrow2 - i, 3).Value - Cells(endrow2 - i, 6).Value), 0)
Next

For i = 23 To 1 Step -1
r1.Cells(endrow2 + 1 - i, 7).Value = Abs(r1.Cells(endrow2 + 1 - i, 4).Value - Application.Average(r1.Range("C" & (endrow2 - 23) & ":" & "C" & endrow2)))
r1.Cells(endrow2 + 1 - i, 8).Value = Abs(r1.Cells(endrow2 + 1 - i, 5).Value - Application.Average(r1.Range("C" & (endrow2 - 23) & ":" & "C" & endrow2)))
r1.Cells(endrow2 + 1 - i, 9).Value = Abs(r1.Cells(endrow2 + 1 - i, 6).Value - Application.Average(r1.Range("C" & (endrow2 - 23) & ":" & "C" & endrow2)))
Next


'Moving Averages until last line of Item
For i = 23 To 2 Step -1
r1.Cells(endrow2 + 3 - i, 10).Value = Round(Application.Average(r1.Range("C" & (endrow2 - i) & ":" & "C" & (endrow2 - i + 2))), 0)
Next
For i = 23 To 3 Step -1
r1.Cells(endrow2 + 3 - i, 11).Value = Abs(r1.Cells(endrow2 + 3 - i, 10).Value - Application.Average(r1.Range("C" & (endrow2 - 22) & ":" & "C" & endrow2)))
Next
'Calculate Averages of MAD and MA MAD
r1.Cells(endrow2 + 2, 7).Value = Application.Average(r1.Range("G" & (endrow2 - 22) & ":" & "G" & endrow2))
r1.Cells(endrow2 + 2, 8).Value = Application.Average(r1.Range("H" & (endrow2 - 22) & ":" & "H" & endrow2))
r1.Cells(endrow2 + 2, 9).Value = Application.Average(r1.Range("I" & (endrow2 - 22) & ":" & "I" & endrow2))
r1.Cells(endrow2 + 2, 11).Value = Application.Average(r1.Range("K" & (endrow2 - 20) & ":" & "K" & endrow2))

'Get R.O.P
'r1.Cells(endrow2 + 3, 2).Value = Application.Min(r1.Cells(endrow2 + 2, 7).Value, r1.Cells(endrow2 + 2, 8).Value, r1.Cells(endrow2 + 2, 9).Value)
m = Application.Match(Application.Min(r1.Cells(endrow2 + 2, 7).Value, r1.Cells(endrow2 + 2, 8).Value, r1.Cells(endrow2 + 2, 9).Value, r1.Cells(endrow2 + 2, 11).Value), r1.Range("G" & (endrow2 + 2) & ":" & "K" & (endrow2 + 2)), 0)
If m = 1 Or m = 2 Or m = 3 Then
r1.Cells(endrow2 + 3, 2).Value = r1.Cells(endrow2 + 1, 3 + m).Value
Else: r1.Cells(endrow2 + 3, 2).Value = r1.Cells(endrow2 + 1, 5 + m).Value
End If

'BOLD STUFF
r1.Cells(endrow2 + 1, 7).EntireRow.Font.Bold = True
r1.Cells(endrow2 + 2, 7).EntireRow.Font.Bold = True
r1.Cells(endrow2 + 3, 7).EntireRow.Font.Bold = True
Next
End Sub

我已经确认r1和r2都存在(运行一个较小的脚本来在2之间传输数据并且它们有效)。我相信这是EndROw的一个问题。但我不确定它是什么。我在r2上有1个填充行,所以我不确定。

我已将星号放在它出错的行上。任何想法都表示赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

问题是您的变量endrow2指向工作表限制$A$1048576的末尾,因此无法在For / Next循环中添加行:r1.Cells(endrow2 + 1, 1).Value

尝试在endrow2的第一个定义上使用xlUp而不是xlDown,因为r1工作表“Sheet4”以第一个单元格A1中的一个非空值开头。

ad2 = r1.Range("A1").End(xlUp).Address
endrow2 = Range(ad2).Row

或者,请调查.UsedRange.End(xlDown).Row

的使用情况