VBA code for cut and paste in a loop

Georgia1

New Member
Joined
Sep 11, 2011
Messages
7
Hi Knowledgeable people

I am a novice Excel VBA user who is trying to create a cut and paste procedure that moves every second cell in a column of data up adjacent to the first cell and then deletes the blank line.

ie. Raw data in column A is:
Number 1
Description 1
Number 2
Description 2
Number 3
Description 3
etc

I want to move "description 1" into Column B adjacent to "Number 1" and then delete the empty line. Then move "description 2 into Column B adjacent to "Number 2" and delete the empty line. etc

I managed to write a macro to do this as a one off, but I get into horrible trouble when I try to repeat it. I just don't know what I am doing the items in under "number" are not different (in terms of the types of characters) to those under description. I suspect it is a backwards loop that I need and I don't know how to write that and put a cut, paste and delete procedure in the middle of it.

Many thanks to anyone who can start me on the right track

Georgia
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Perhaps a bit technically lacking, but this seems to work (only according to your specs)..

Code:
Sub Foo()
Dim i As Long, LR As Long
Application.ScreenUpdating = False
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR).Select
Do
ActiveCell.Cut
On Error Resume Next
ActiveCell.Offset(-1, 1).Select
ActiveSheet.Paste
On Error GoTo errHandler
ActiveCell.Offset(-1, -1).Select
Loop Until ActiveCell = Range("A1")
Exit Sub
errHandler:
Range("A" & LR).Select
For i = LR To 2 Step -1
    If Cells(i, 1).Value = "" Then Cells(i, 1).EntireRow.Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Jim
It worked! Thankyou so much for your assistance.

I'll go through it and see if I can work out how it all works!

Georgia :)
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,785
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