VBA复制范围重复

时间:2018-10-22 18:30:04

标签: excel vba copy range rows

我试图在VBA中选择14行,我知道电表的数量(起始单元格)。该行在表中重复。我不明白我要用这段代码

Sub test()
Dim LastRow As Long


 Set sh = Sheets("Sheet1")

    LastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Worksheets("Sheet1").Activate

  For x = 0 To LastRow

    If Worksheets("Sheet1").Cells(x, 2) = "58117552" Then                       
         Range(Cells(x, 2), Cells(X, 2)+14).copy
    End If
  Next 
End sub

感谢帮助。

https://www.postgresql.org/docs/9.6/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE

1 个答案:

答案 0 :(得分:1)

由于您总是想互相复制15行,并且在您的示例中,数字“Numérode Compteuer”总是出现在2,16,30(每15步),因此您可以跳过搜索功能而只复制每行第15行以及下面的内容。

您需要定义要将复制的范围粘贴到的位置...我刚刚使用了Sheet2。

以以下内容开头:

Sub test()
Dim LastRow As Long
Dim sh As Worksheet
Dim sh2 As Worksheet
Dim X As Long

Set sh = Sheets("Sheet1") 'Define sheet to copy from
Set sh2 = Sheets("Sheet2") 'Define sheet to copy to

LastRow = sh.Cells(Rows.Count, 2).End(xlUp).Row 'Find last row to copy from

For X = 2 To LastRow Step 15 'Jump 15 step each time (since you are interested in every 15 row)
    If Worksheets("Sheet1").Cells(X, 2) <> "" Then 'If cell is empty then
         Range(sh.Cells(X, 2), sh.Cells(X + 14, 2)).Copy 'Copy the range, where 15 rows downward will be copied each time
         sh2.Range(sh2.Cells(X, 5), sh2.Cells(X + 14, 5)).PasteSpecial xlPasteValues 'Paste somewhere
    End If
  Next
Application.CutCopyMode = False
End Sub