范围的方法Object_Worksheet在将数据从工作表复制和粘贴到另一个工作表时失败

时间:2016-03-09 17:25:36

标签: excel vba excel-vba

我正在尝试将数据从输入表复制并粘贴到输出表中,一旦数据在下一个电子表格中,它将从开始日期到结束日期的下一行填充。

原样,代码在调试时遇到以下错误: ws1.Range(" A"& NextRow)=范围Object_Worsheet的方法失败;同样适用于ws1.Range(" B"& NextRow); ws1.Range(" C"& NextRow)等......

ws1.Cells(LastRow," H")=应用程序定义或对象定义的错误。

另外,我注意到当我设置RawDataEntries时它等于37,这是最后一个非空行,但是当我尝试使用For循环并将鼠标指向RawDataEntries时,For n = 1 To RawDataEntries,VBA给我一个值105,这似乎无处不在。

我认为代码背后的逻辑是正确的。可能出了什么问题?

Sub AddFlight_Click()
Const RNG_END_DT As String = "N2"
Dim NextRow1 As Long, LastRow1 As Long, ws1 As Worksheet
Dim ws2 As Worksheet, RawDataEntries As Long


Set ws1 = Sheets("JetAir Flight Plan")
Set ws2 = Sheets("TUI B Flight Plan")
LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
NextRow1 = LastRow1 + 1
RawDataEntries = ws2.Range("A" & Rows.Count).End(xlUp).Row

For n = 1 To RawDataEntries

'Data from an input worksheet is copied and pasted into specific cells in an output worksheet.

    ws1.Range("A" & NextRow).Value = ws2.Range("A" & n).Value
    ws1.Range("B" & NextRow).Value = ws2.Range("B" & n).Text
    ws1.Range("D" & NextRow).Value = ws2.Range("D" & n).Text
    ws1.Range("E" & NextRow).Value = ws2.Range("E" & n).Text
    ws1.Range("F" & NextRow).Value = ws2.Range("F" & n).Text
    ws1.Range("G" & NextRow).Value = ws2.Range("G" & n).Text
    ws1.Range(RNG_END_DT).Value = ws2.Range("H" & n).Value

'A series of dates is created from a starting date
'    to an ending date in column A of ws1.

    ws1.Range("A" & NextRow).DataSeries Rowcol:=xlColumns, _
        Type:=xlChronological, Date:=xlDay, Step:=7, _
        Stop:=ws1.Range(RNG_END_DT).Value, Trend:=False

'The data filled in the last row with the userform data through
' the first part of the macro will be copied and pasted in
' the next row until there is a blank cell in column A.

    LastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws1.Range(ws1.Range("B" & NextRow), ws1.Cells(LastRow, "H")).FillDown

'We repeat the process for other rows on the sheet data are pulled from

Next n

非常感谢。

1 个答案:

答案 0 :(得分:0)

将NextRow更改为NextRow1。现在你正在调用一个不存在的变量。

或者你可以做相反的事情(NextRow1到NextRow)

与LastRow和LastRow1相同