在VBA Excel中复制粘贴范围

Int*_*n87 2 excel vba excel-vba

我试图将一行值粘贴从一个工作表复制到另一个工作表,但不断出现运行时错误1004:应用程序定义或对象定义的错误.错误是在下面两个的第一行,我不知道我哪里出错了.

Set copyRange = Worksheets("Sheet2").range(A & i + 1 & CA & i + 1)
copyRange.Copy Destination:=Cells(countD, 2)
Run Code Online (Sandbox Code Playgroud)

代码需要一次复制一行并将其粘贴到默认工作表中.

编辑 完整代码

Dim List1 As range
Dim List2 As range
Dim lastRow As Integer
Dim countD As Integer
Dim found As Boolean
Dim copyRange As range

Set List1 = ThisWorkbook.Sheets("Sheet1").range("H2:H600")
Set List2 = ThisWorkbook.Sheets("Sheet2").range("I2:I600")
countD = 2
lastRow = Application.CountA(ThisWorkbook.Sheets("Sheet2").range("C:C"))

For i = lastRow To 2 Step -1
    found = False
    value1 = List1.Item(i, 1)
    For Each value2 In List2
        If value1 = value2 Then
            found = True

            Exit For
        End If
    Next

    If found = False Then
        Set copyRange = Sheets("Sheet1").range("A" & i + 1 & "CA" & i + 1)
        copyRange.Copy Destination:=Cells(countD, 2)
        Sheets("Discrepancies").Cells(countD, 1) = "name not found"
        ThisWorkbook.Sheets("Sheet1").Cells(i + 1, 1).EntireRow.Delete
        Cells(countD, 8).Interior.ColorIndex = 3

        countD = countD + 1

    End If

Next
Run Code Online (Sandbox Code Playgroud)

why*_*heq 6

正如瓦西姆的评论所提到的那样 - :在前面添加了一个冒号CA

Sub copyRangeOver()

Dim i As Integer
i = 6

Dim copyRange  As Range
Set copyRange = ThisWorkbook.Worksheets("Sheet2").Range("A" & i + 1 & ":CA" & i + 1)

Dim countD As Integer
countD = 10
copyRange.Copy Destination:=Cells(countD, 2)


End Sub
Run Code Online (Sandbox Code Playgroud)

  • (@Vasim抱歉回复并粘贴你的评论!......让我看看我是否可以补偿你......); 现在你已经跳到了300分! (2认同)