0

I am trying to write VBA code that will select a named range, copy it and paste it for a certain number of rows. What I need to know is how to select the range of cells corresponding to the ones above.

E.g. I have a range "myRange" which refers to: "=$A$1:D$1$,$F$1,$K$1". I want to copy this and paste it in "=$A$2:D$2$,$F$2,$K$2" by referring to "myRange" in stead of to the string of cell references.

Any help?

1 Answer 1

1

Something like this?

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

EDIT: OK, something like this then (to cope with disjoint ranges):

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
2
  • Doesn't work, I need something that will function on multiple selections such as "=$A$1:D$1$,$F$1,$K$1"
    – Karl
    Jul 29, 2009 at 10:25
  • Updated to cater for disjoint ranges. Jul 29, 2009 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.