Excel 应用程序定义或对象定义错误。从工作表复制到另一工作簿中的工作表

Excel 应用程序定义或对象定义错误。从工作表复制到另一工作簿中的工作表,excel,vba,runtime-error,Excel,Vba,Runtime Error,我有一段代码检查文件是否打开,并根据该标准复制和粘贴单元格信息。当程序运行时,我收到一条错误消息,上面写着“应用程序定义的或对象定义的错误”。抛出错误的行将被注释 ' check if the file is open Ret = Isworkbookopen("\\showdog\service\Service Jobs.xlsm") If Ret = False Then ' open file Set wkbDest = Workbooks.Open("\\showdog\service

我有一段代码检查文件是否打开,并根据该标准复制和粘贴单元格信息。当程序运行时,我收到一条错误消息,上面写着“应用程序定义的或对象定义的错误”。抛出错误的行将被注释

' check if the file is open

Ret = Isworkbookopen("\\showdog\service\Service Jobs.xlsm")
If Ret = False Then
' open file
Set wkbDest = Workbooks.Open("\\showdog\service\Service Jobs.xlsm")
Set destSheet = wkbDest.Sheets("Customer Information")
'perform copy
Set shttocopy = wkbSource.Sheets("Report")

shttocopy.Range("A11:J11").Resize(xlDown).Copy       'error is on this line 

destSheet.Range("A4:J4").Cells(xlDown).PasteSpecial


Application.DisplayAlerts = False

wkbDest.Save
wkbDest.Close

Application.DisplayAlerts = True

我做错了什么?

这里有一个代码可以帮助您开始:

Sub test()
Dim lastRow As Integer, lastCol As Integer, startRow As Integer
Dim ws As Worksheet, newWS As Worksheet

Set ws = ActiveSheet

startRow = 11 'starting in row 11 - you can change this.

'Create and name the new worksheet, which you will paste into.
Sheets.Add After:=Sheets(Sheets.Count) 'add to end of workbook
ActiveSheet.Name = "New Sheet"
Set newWS = Sheets("New Sheet")

' Starting in cell A11, what's the last row in that group of cells?
With ws
    If IsEmpty(.Cells(startRow + 1, 1)) Then ' this will check to see if there's only one row of data.
            lastRow = startRow ' If A12 is blank, you don't want last row to be 66000, so just set 11 as the last row
        Else
            lastRow = .Cells(startRow, 1).End(xlDown).Row
    End If
    ' Starting in cell A11, how many columns to the right are there?
    lastCol = .Cells(startRow, 1).End(xlToRight).Column

    ' Now, let's copy your range - NOTE: You don't need copy, so I commented this out.  But this would copy it if you didn't.
    ' .Range(.Cells(startRow, 1), .Cells(lastRow, lastCol)).Copy
End With

' Paste the data into the new sheet
Dim newLastRow As Integer 'what row is the starting row for your info?
newLastRow = newWS.Cells(66000, 1).End(xlUp).Row + 1 'starting in A66000, what's the next row with info? Add one because you want to start on the next blank row

newWS.Range(newWS.Cells(startRow, 1), newWS.Cells(lastRow, lastCol)) = ws.Range(ws.Cells(startRow, 1), ws.Cells(lastRow, lastCol)).Value
Application.CutCopyMode = False

End Sub
您不一定需要复制/粘贴-您可以将一个范围的值设置为另一个范围的值

不知道为什么要粘贴到新工作表的第4行,我只是将数据复制到与原始工作表相同的范围。如果您让我知道为什么第4行(这是从第1行开始的“下一个”开放行吗?),我可以做一些调整

我希望代码是有意义的,我试着去评论它。当然,您需要将其归入您的变量中,但它应该可以让您继续


至于resize(),我很确定您必须同时声明列和行的大小调整。如果不调整宽度(列)或高度(行),只需将“1”作为调整大小的数字。

您打算如何处理该范围(A11到J11)?将一个范围从第11行复制到下一行,其中包含信息(使用resize)?这是别人向我建议的,因为我开始没有办法解决这个问题。其概念是获取该范围并遍历列,复制每行中的所有信息(总数未知),然后将其粘贴到另一张表中。现在我想起来了,我不确定我在描述中是否明确了这一点。我也尝试用“值”来代替“调整大小”。我可以让它用“end”方法粘贴最后一个单元格,但我不知道如何粘贴整个单元格。