How to shift rows without copy paste

Shivi0189

New Member
Joined
Apr 1, 2019
Messages
8
I have a large no. of entries in excel like below:

BOAT
NO
LOT NODensity STATUS
F2/146582010.4810.4910.4810.510.49 ACC
10.4910.4910.49
F2/149582010.4710.4710.4710.4710.44 RS
10.4710.4710.47
F2/150582010.4710.4710.4910.4510.47 ACC
10.4710.4610.48
F2/153584310.5310.5310.5410.5310.56 ACC
10.5510.5510.55
F2/154584310.5210.5310.5210.5210.51 ACC
10.5110.5210.52

<colgroup><col span="2"><col><col span="5"></colgroup><tbody>
</tbody>

<colgroup><col span="2"><col><col span="5"></colgroup><tbody>
</tbody>
The boat no. has been assigned a lot no. & 8 density values are recorded in two rows. I want them to arrange in single row against each boat & lot no. for statistical analysis.
Any help will be of great use.
Thanks in advance!
 

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.
Hi,

I have come up with somewhat of a solution (at least it worked for the example data provided from your post)

Firstly, create 3 new columns by insertion from highlighting the Status column (Column H) so that Status now shows in column K and H,I,J are now blank.

In Column L I created a helper formula, e.g. in cell L3:
=COUNT(C3:G3)
which returns 5 or 3 - drag this down all of the rows until the bottom row.

Then I made this code to cut and paste where the helper formula = 3 to the above line where helper formula = 5

Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim i As Long
    Dim lastRowL As Long
    
    lastRowL = Cells(Rows.Count, 12).End(xlUp).Row
    For i = 3 To lastRowL
        If Cells(i, 12).Value = 3 Then
            Range(Cells(i, 3), Cells(i, 5)).Select
            Selection.Cut
            Cells(i - 1, 8).Select
            ActiveSheet.Paste
        End If
    Next i
    
End Sub

I hope this helps with a larger data set, let me know!

There are definitely more efficient ways but I can't think of them with my current knowledge level! :P

Once this is done you can then delete the helper column if you wish.

I'd save before running this just incase, or make a backup file!!
 
Last edited:
Upvote 0
Another option:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Apr49
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Application.ScreenUpdating = False
Lst = Range("C" & Rows.Count).End(xlUp).Row
Range("H:H").Resize(, 3).Insert
    [COLOR="Navy"]For[/COLOR] n = Lst To 3 [COLOR="Navy"]Step[/COLOR] -2
        Range("C" & n).Resize(, 3).Cut Range("H" & n - 1)
    [COLOR="Navy"]Next[/COLOR] n
Range("A1").Resize(Lst).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Just noticed this code leaves a row gap between each boat No.

This can be changed if the code is edited to say:
Rows(i).Delete Shift:=xlUp
After the "ActiveSheet.Paste"
but before End If
 
Upvote 0
Thank you but "Then I made this code to cut and paste where the helper formula = 3 to the above line where helper formula = 5" I am unable to understand this line.
I am beginner in VBA and may not be able to catch whatever you are trying to explain. A little elaboration may help.
 
Upvote 0
Thank You! This helped but not completely. I am getting mixed results like below:

F3/356606510.6910.69ACC
F3/357606510.6710.68ACC
F3/358606510.6610.65ACC
F3/359606510.6810.67ACC
F3/360606510.7310.6710.6810.6710.5610.6710.3210.67REJ
F3/361606710.6610.6210.6610.6310.6110.6710.6310.66ACC

<colgroup><col span="2"><col><col span="8"></colgroup><tbody>
</tbody>

Some of the data is getting converted to desired results but in between I am loosing data for some enteries. I am new to VBA so I'll tell you how I did.
I copied the code & in my worksheet I pressed ALT+F11. I went to Insert option, then module then pasted the code. Went to file option and clicked on "close & return to MS Excel". In my workbook, I clicked view->Macros->view macros->run.
The first workbook I tried in, I could get the results but another workbook I got something like shown above.
 
Upvote 0
If I am not mistaken, I think this non-looping macro will do what you want...
Code:
Sub Densities()
  Application.ScreenUpdating = False
  Columns("H").Resize(, 3).Insert
  With Range("H2:J" & Cells(Rows.Count, "C").End(xlUp).Row)
    .FormulaR1C1 = "=R[1]C[-5]"
    .Value = .Value
  End With
  Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option:-
Code:
[COLOR=Navy]Sub[/COLOR] MG01Apr49
[COLOR=Navy]Dim[/COLOR] n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Lst [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
Application.ScreenUpdating = False
Lst = Range("C" & Rows.Count).End(xlUp).Row
Range("H:H").Resize(, 3).Insert
    [COLOR=Navy]For[/COLOR] n = Lst To 3 [COLOR=Navy]Step[/COLOR] -2
        Range("C" & n).Resize(, 3).Cut Range("H" & n - 1)
    [COLOR=Navy]Next[/COLOR] n
Range("A1").Resize(Lst).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

Hey! The code works perfectly fine. There were some mistakes in my workbook itself. Thank a lot!
 
Upvote 0

Forum statistics

Threads
1,215,184
Messages
6,123,533
Members
449,106
Latest member
techog

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