Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

STL - Formerly Best Training Solutions Through Learning
TrustPilot
Excellent
Request Callback We will call you back
0207 987 3777 Call for assistance
Your Basket Basket is empty
copy cells using do

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Copy cells by using do while

Copy cells by using do while

ResolvedVersion 2007

Carmen has attended:
Excel VBA Intro Intermediate course

Copy cells by using do while

I am trying to copy the value in cell C9 into the next 4 rows vertically. After that copy again the value in cell C14 and paste it into the next 4 rows, after the value in C19 and paste it into th next 4, by repeating always the same sequence. I would like to repeat that process all along the column and being able to stop it when there is no more values after counting 4 rows.

I have done this so far, but the macro never stops. Any suggestions to make it work?

Range("C9").select
a = activecell.value
do until a = ""
do until activecell.value <> a
If activecell.offset (1,0) = "" Then
selection.copy
activecell.offset(1,0).select
activesheet.paste

else

activecell.offset (1,0).select

End if

Loop
a =activecell.value

Loop

End sub

RE: copy cells by using do while

Hi Carmen

Thanks for getting in touch. There's a bit of a clash in the code as the loop stops when there is an empty cell immediately below the ActiveCell, however it's only going to copy when there is an empty cell below the ActiveCell.

Does the following help?

Sub Paste4Cells()

Range("C9").Select

Do Until ActiveCell.Offset(4, 0) = ""

a = ActiveCell.Value

For i = 1 To 3

ActiveCell.Offset(i, 0) = a

Next i

ActiveCell.Offset(4, 0).Select

Loop

End Sub

* The loop runs until the 4th cell down is empty

* When it picks up a value, it copies it to the three cells underneath

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: copy cells by using do while

Amazing. It works perfectly.

Thank you very much

Regards

Carmen

Excel tip:

Turn off AutoComplete in Excel

You may have noticed when typing into your spreadsheets that if you start to enter labels that begin with the same letters as a label that has been previously entered in the same spreadsheet, Excel will try and automatically complete the text for you. This feature is called AutoComplete.

If you find this feature more annoying than useful, you can turn it off by:

1. Going to Tools - Options.

2. Select the Edit tab.

3. Remove the tick from next to the "Enable AutoComplete for cell values" option.

4. Click OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.