How to loop copy / paste until cell in range IsEmpty

brentix

New Member
Joined
Sep 17, 2010
Messages
25
I'm only just now starting to do Loops (i've so far managed to avoid them). However i now want to create a loop that will loop through a range until it finds a cell that is empty.

For each cell that is not empty I want it to copy and paste to maintain the formulas ine sheet as part of an edit / delete system.

Range would be Range("A5:A200")

the following code is copy and paste for 1 row where the cell shows a number (i'd prefer it to check for any value - but i'll cover that when i have a working loop)
Code:
range("A5").Select 'starting point
    If IsNumeric(Selection.Value) Then
    Selection.Offset(-1, 1).Copy
    Selection.Offset(0, 1).PasteSpecial
    Selection.Offset(-1, 1).Copy
    Selection.Offset(0, 1).PasteSpecial
    Selection.Offset(-1, 1).Copy
    Selection.Offset(0, 1).PasteSpecial
    Selection.Offset(-1, 1).Copy
    Selection.Offset(0, 1).PasteSpecial
    Selection.Offset(-1, 1).Copy
    Selection.Offset(0, 1).PasteSpecial
    End If

Essentially i want it to loop through the range until the first empty cell but I just can't get my head around it, any help or pointers would be much appreciated as I just can't figure out where to start.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Is this what you want?
Code:
Dim i as Long
i = 5
Do While Not IsEmpty(Range("A" & i))
     If IsNumeric(Range("A" & i)) Then
        Range("B" & i-1, "F" & i-1).Copy
        Range("C" & i).PasteSpecial Paste:=xlValues
     End If
     i = i + 1
Loop
 
Last edited:
Upvote 0
Fantastic, it was almost there but you helped me figure out exactly what i needed so big thanks, i've attached the code that works for me
Code:
Dim i As Long
i = 2
Do While Not IsEmpty(range("A" & i))
     If IsNumeric(range("A" & i)) Then
        range("B" & i - 1, "B" & i).Copy
        range("B" & i).PasteSpecial Paste:=xlValues
        range("C" & i - 1, "C" & i).Copy
        range("C" & i).PasteSpecial Paste:=xlValues
        range("D" & i - 1, "D" & i).Copy
        range("D" & i).PasteSpecial Paste:=xlValues
        range("E" & i - 1, "E" & i).Copy
        range("E" & i).PasteSpecial Paste:=xlValues
        range("F" & i - 1, "F" & i).Copy
        range("F" & i).PasteSpecial Paste:=xlValues
     End If
     i = i + 1
Loop

I knew I was nissing something, I hadn't even considered the
Code:
Do While Not IsEmpty(range("A" & i))
part
This a huge help so big thanks again as this is also a big insight into other Loops i'll make in future.
 
Upvote 0
You could use this instead:
Code:
Application.ScreenUpdating = False
Dim i As Long
i = 2
Do While Not IsEmpty(Range("A" & i))
     If IsNumeric(Range("A" & i)) Then
        Range("B" & i - 1, "F" & i).Copy
        Range("B" & i).PasteSpecial Paste:=xlValues
     End If
     i = i + 1
Loop
Application.ScreenUpdating = True
 
Upvote 0
good point i missed that one
your version duplicates the line past the value row if you press the button again so amended
range("B" & i - 1, "F" & i).Copy
with
range("B" & i - 1, "F" & i - 1).Copy
now it doesn't go past the last value row
Code:
Dim i As Long
i = 2
Do While Not IsEmpty(range("A" & i))
     If Not IsEmpty(range("A" & i)) Then
        range("B" & i - 1, "F" & i - 1).Copy
        range("B" & i).PasteSpecial Paste:=xlValues
     End If
     i = i + 1
Loop
it's perfect now cheers
 
Upvote 0
just one other observation
if you use
range("B" & i).PasteSpecial
rather than
range("B" & i).PasteSpecial Paste:=xlValues

you can use it for updating and maintaining formulas

I love it when a plan comes together :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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