简体   繁体   中英

Copy paste loop a number of times as the cell value - VBA

I am trying to build a loop to copy and paste a range of cells from one excel worksheet to another a number of times, as specified in a cell A1.

    Sub AnalogPointBuild()

                Dim i As Integer
                Dim y As Integer

                'only copy the number of times as the count in cell A1 of worksheet "inputs"
                Sheets("inputs").Activate
                y = Range("A1").Value

                ' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"        

                For i = 1 To y
                Worksheets("Analog Template").Range("B3:EU3").Copy Worksheets("Analog Output").Range("B3:EU3")
                Next i

    End Sub

I understand that during the iteration, the paste does not increment the cell value and paste it in the next one down.

Help! What's wrong with my code?

Try this, as per comments above.

Sub AnalogPointBuild()

Dim y As Long

'only copy the number of times as the count in cell A1 of worksheet "inputs"
y = Sheets("inputs").Range("A1").Value

' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"
Worksheets("Analog Output").Range("B3:EU3").Resize(y).Value = Worksheets("Analog Template").Range("B3:EU3").Value

End Sub

You don't need a for loop, rather you can try it like this...

Sub AnalogPointBuild()
    Dim y As Integer

    'only copy the number of times as the count in cell A1 of worksheet "inputs"
    y = Sheets("inputs").Range("A1").Value

    ' copy the range of cells from one worksheet "Analog Template' to the "Analog Output"

    Worksheets("Analog Template").Range("B3:EU3").Copy Worksheets("Analog Output").Range("B3:EU3").Resize(y)

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM