0

我在此代码上不断收到错误 1004“复制区域的大小/形状不同”:

我试过在一些地方插入各种 .pastespecials、.selection.autofit ......但我有点在黑暗中刺伤。

帮助?

Sub SaveData()

Dim rr As Range
Set rr = Selection

For Each Row In rr

   'error occurs here'
    Range("a" & i & ":BN" & i).Copy Worksheets("ArchiveData").Cells(Worksheets("ArchiveData").Rows.Count, "b").End(xlUp).Offset(1, 0) 
    Sheets("ArchiveData").Cells(Worksheets("archivedata").Cells(Rows.Count, 2).End(xlUp).Row, 1).Value = Date



    Next Row

结束子

4

2 回答 2

0

首先使用 OPTION EXPLICIT!!!(工具 > 选项 > 编辑器 > 需要变量声明)

现在到这个特定的问题(如果启用 Option Explicit 就可以清楚地看到这一点。

Row inFor Each Row In rr被视为变量或对象,而不是范围 rr 的行。

你需要使用的是

Dim rr As Range
Dim rrrow as Range

Set rr = Selection

For Each rrrow In rr.Rows

现在下一个问题也i没有标注,甚至没有值,因此它很可能为零或空(0或“”或NULL)

我猜你想要进行类似的事情:

rrrow.Copy
With Worksheets("ArchiveData")
    .Range(........).PasteSpecial 'HERE YOU ONLY NEED TO SPECIFY THE TOP LEFT MOST CELL
End With
于 2013-03-25T21:33:11.423 回答
0

试试下面的代码:

Sub SaveData()

    Dim rr As Range
    Set rr = Selection


    Dim wsht As Worksheet
    On Error Resume Next
    Set wsht = Worksheets("ArchiveData")
    On Error GoTo 0

    If wsht Is Nothing Then Exit Sub

    For i = 1 To rr.Rows.Count

        Range("a" & i & ":BN" & i).Copy wsht.Cells(wsht.Range("B65000").End(xlUp).Row + 1, 2)
        wsht.Cells(wsht.Cells(Rows.Count, 2).End(xlUp).Row, 1).Value = Date

    Next
End Sub
于 2013-03-26T00:49:45.913 回答