0

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?

2
  • 2
    You are not changing your paste destination. Plus you don't need to loop (or copy and paste) - use Resize. Commented Jan 5, 2018 at 17:39
  • 1
    Watch what happens if you use F8 to step through your code as the for loop executes.. Commented Jan 5, 2018 at 17:43

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I did not know about the resize property! Much appreciated, thank you, @SJR!
Very useful it is. Use it whenever you can!
1

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.