首页 > 解决方案 > 如何复制超出范围的数据

问题描述

我正在尝试从我使用两个字符串设置的范围中复制一些作为列的数据。范围在“B”列中设置,我需要将“C”和“D”列中的数据复制到范围的长度,并将它们粘贴到“B”和“C”列中的另一张表中。

该范围是通过查找两个字符串“实施阶段的初始项目 ER”和“25 年的初始项目 ER”来设置的。

到目前为止,我已经设法编写将数据复制到正确位置的代码,但它只是复制范围(列“B”)中的数据

fr = "Originating Project ERs at Implementation Stage"
fc = "Originating Project ERs at 25"
Set r = Worksheets("Sheet1").Cells.Find(What:=fr, LookAt:=xlWhole)
Set c = Worksheets("Sheet1").Cells.Find(What:=fc, LookAt:=xlWhole)

If Not r Is Nothing Then
    StartR = r.Row + 1
        Else: MsgBox fr & " not found"

End If

If Not c Is Nothing Then
    EndR = c.Row - 1
        Else: MsgBox fc & " not found"
End If

If r.Row And c.Row > 1 Then
Worksheets("Sheet1").Range(r, c).Offset(1,1).Copy
Worksheets("PriorityProgress").Range("B2").PasteSpecial Paste:=xlPasteValues
Worksheets("priorityProgress").Range("C2").PasteSpecial Paste:=xlPasteValues

End If`

这是一些示例数据,显示了我想要达到的目标https://i.stack.imgur.com/5csrZ.png

编辑:我已经设法使用 OffSet 1,1 在“C”列中显示第一组记录,现在只需要“D”

标签: excelvba

解决方案


设法通过单独偏移每一列来解决它

 If r.Row And c.Row > 1 Then
    Worksheets("Sheet1").Range(r, c).Offset(1, 1).Copy
    Worksheets("PriorityProgress").Range("B2").PasteSpecial Paste:=xlPasteValues
    Worksheets("Sheet1").Range(r, c).Offset(1, 2).Copy
    Worksheets("priorityProgress").Range("C2").PasteSpecial Paste:=xlPasteValues

推荐阅读