Copy and Paste Loop Error

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
89
Office Version
  1. 2010
Platform
  1. Windows
Hi Gurus,

Can anyone help with below?

The task is to do a loop, starting on cell F4 in Extract sheet and do the following actions:
-Copy the cell in F4 in Extract sheet F4 and paste this into Test Sheet B2
-Then go back to Extract sheet F4 and paste this into Test Sheet B2
-Continue this loop based on the CountA formula in Extract sheet F1 (ie if F1 value was 20, it will do this copy and paste through to F4, F5, F6....F23)

I have the below code but its not working.. think i haven't declared something or missed it...


Dim i As Long
Dim CountinF1 As Integer
CountinF1 = Sheets("Extract").Range("F1").Value


For i = 1 To CountinF1
'start from F3 and then move onto F4,F5, F6 until end of CountinF1

Sheets("Extract").Range("F4").Select 'after one loop it gets stuck here with a run time error
Selection.Copy
Sheets("Test Sheet").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues

Next i
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If F1 is 20 do you want to copy F4 to B2 twenty time, or F4 To B2, then F5 to B2, or maybe F4 to B2, F5 to B3 etc?
 
Upvote 0
No sorry - just copy once and then move f2 to b2, then f3 to b2 and so on.

I guess I meant the looping should go down to F23 based on F1 being 20.
 
Upvote 0
Ok, how about
VBA Code:
Dim i As Long
Dim CountinF1 As Long

With Sheets("Extract")
   CountinF1 = .Range("F1").Value
   For i = 1 To CountinF1
      Sheets("Test Sheet").Range("B2").Value = .Range("F" & i + 3).Value
   Next i
End With
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top