VBA复制范围增加了偏移

时间:2018-10-20 10:41:28

标签: excel vba excel-vba copy offset

我试图复制一个范围(行)在VBA中重复几次,我知道起始单元格的名称和范围的大小。

Sub test()
  Dim LastRow As Long, x As Long, a As Long, b As Long
  a = Sheets("sd").Cells(Rows.Count, 1).End(xlUp).Row
  For x = 1 To a
     If Worksheets("sd").Cells(x, 2) = "58117552" Then
      Sheets("sd").Range(Cells(x, 2), Cells(x, 2).Offset(13, 0)).entireRows.Copy
      Sheets("sheet1").Activate
      Sheets("sheet1").Cells(1, 1).Select
      Sheets("sheet1").Paste
      Sheets("sheet1").Activate
    End If
  Next
End Sub

显然,此代码无法正常工作,但我不知道选择此范围的正确方法

感谢帮助

1 个答案:

答案 0 :(得分:0)

offset(13)有点令人困惑,但这是删除了选择的代码。

Sub test()
    Dim a As Long
    Dim sh As Worksheet, ws As Worksheet

    Set sh = Sheets("sd")
    Set ws = Sheets("Sheet1")

    With sh
        a = .Cells(.Rows.Count, 2).End(xlUp).Row
        For x = 1 To a
            If .Cells(x, 2) = "58117552" Then
                .Cells(x, 2).Offset(13, 0).EntireRow.Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
            End If
        Next x
    End With

End Sub