VBA命名范围偏移

时间:2009-07-28 13:28:36

标签: excel vba copy-paste named-ranges

我正在尝试编写将选择命名范围的VBA代码,将其复制并粘贴一定数量的行。我需要知道的是如何选择与上述单元格相对应的单元格范围。

E.g。我有一个范围“myRange”,它指的是:“= $ A $ 1:D $ 1 $,$ F $ 1,$ K $ 1”。我想复制它并将其粘贴在“= $ A $ 2:D $ 2 $,$ F $ 2,$ K $ 2”中,引用“myRange”而不是单元格引用字符串。

任何帮助?

1 个答案:

答案 0 :(得分:1)

这样的东西?

Sub Test()

    Dim oRange As Range
    Set oRange = ActiveSheet.Range("A1:D1") ' Change this to point at the range to be copied

    Dim i As Integer
    For i = 1 To 10
        oRange.Copy
        oRange.Offset(i, 0).PasteSpecial xlPasteAll
    Next i

End Sub

编辑:好的,那样的事情(为了应对不相交的范围):

Sub Test()

    Dim oRange As Range
    Set oRange = ActiveSheet.Range("A1,C1:D1") ' Change this to point at the range to be copied

    Dim i As Integer
    For i = 1 To 10
        Dim oArea As Range
        For Each oArea In oRange.Areas
            oArea.Copy
            oArea.Offset(i, 0).PasteSpecial xlPasteAll
        Next oArea
    Next i

End Sub