excelvba

VBA copy and paste a range multiple times based on cell value


I'm trying to copy and paste a range of cells multiple times based on the number that is listed in another cell (up to 3 times). So If B1 = 2 then I want to copy and paste A2:C11 into A13:C22.If B1 = 3 then copy and paste A2:C11 into A13:C22 AND A24:C33.If B1 = 4 then copy and paste A2:C11 into A13:C22 AND A24:C33 AND A35:C44.

I'm very new to VBA so I appreciate any help!! Thank you!!

enter image description here

Haven't tried anything as I don't really know where to begin.


Solution

  • Sub CopyBlock()
        Dim rng As Range, n As Long
        With ThisWorkbook.Sheets("Sheet1") ' sheet name
            .Rows("13:" & .Rows.Count).Delete
            Set rng = .Range("A2:C11")
            For n = 1 To .Range("B1").Value - 1
               rng.Copy rng.Offset(n * (rng.Rows.Count + 1))
            Next
            MsgBox n - 1 & " blocks copied", vbInformation
        End With
    End Sub