Macro to move cells and delete lines

Midas

New Member
Joined
Sep 10, 2004
Messages
5
Hi, I have a file with 1000+ lines of data which needs to be rearranged. My macro-writing skills are essentially zero, so I am hoping someone on this forum can help.
What I need is a macro which can do the following :
1. In column A move every second line cell to column B at one line higher
2. Same for column G : move every second line cell to column H at one line higher
3. Each time delete the empty line after both cells are moved.

See image for the current and required view. Many thanks for your help !!
 

Attachments

  • Excel macro screenshot.jpg
    Excel macro screenshot.jpg
    129.5 KB · Views: 23

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:'
VBA Code:
Sub moveData()
    Application.ScreenUpdating = False
    Dim lRow As Long, x As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = lRow To 2 Step -2
        Range("B" & x - 1) = Range("A" & x)
        Range("H" & x - 1) = Range("G" & x)
        Rows(x).Delete
    Next x
    Application.ScreenUpdating = False
End Sub
 
Upvote 1
Solution
One way.
VBA Code:
Sub Demo()
    Dim WS As Worksheet, rng As Range, R As Range

    Set WS = ActiveSheet
    With WS
        Set rng = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With

    For Each R In rng
        If R.Row Mod 2 = 0 Then
            R.Offset(-1, 1) = R.Value
            R.Value = vbNullString
            R.Offset(-1, 7) = R.Offset(0, 6)
            R.Offset(0, 6) = vbNullString
        End If
    Next R
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 1
Another way that does not go back and forth to the sheet in the For/Next loop...

VBA Code:
Sub test()

    Dim arr, x As Long, lRow As Long
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    arr = Range("A1:H" & lRow)
    For x = 1 To lRow Step 2
        If x + 1 > lRow Then Exit For
        arr(x, 2) = arr(x + 1, 1)
        arr(x, 8) = arr(x + 1, 7)
    Next

    Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    ActiveSheet.Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
End Sub
 
Upvote 0
Try:'
VBA Code:
Sub moveData()
    Application.ScreenUpdating = False
    Dim lRow As Long, x As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For x = lRow To 2 Step -2
        Range("B" & x - 1) = Range("A" & x)
        Range("H" & x - 1) = Range("G" & x)
        Rows(x).Delete
    Next x
    Application.ScreenUpdating = False
End Sub
This works perfect ! All data is arranged as required in a blink of an eye !! Many thanks !!!
 
Upvote 0
One way.
VBA Code:
Sub Demo()
    Dim WS As Worksheet, rng As Range, R As Range

    Set WS = ActiveSheet
    With WS
        Set rng = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With

    For Each R In rng
        If R.Row Mod 2 = 0 Then
            R.Offset(-1, 1) = R.Value
            R.Value = vbNullString
            R.Offset(-1, 7) = R.Offset(0, 6)
            R.Offset(0, 6) = vbNullString
        End If
    Next R
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
This works too ! Thank you so much !! You made my day !!!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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