0

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

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

有什么帮助吗?

4

1 回答 1

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
于 2009-07-28T13:38:37.440 回答