VBA code with IF, cut/paste, & last column

RLCornish

New Member
Joined
Feb 11, 2014
Messages
42
I have a list of travel details where each row is a different leg of a travelers journey. There are common details at the beginning of the row, such as passenger name and such. In order to use the data to auto-fill a templated itinerary I need to align the legs of the journeys per passenger into a single row. Here's an example of what the data I receive looks like...

DOE/JOHN119/01/1719/01/17Duluth, MNMinneapolis, MN7:00AM
DOE/JOHN219/01/1719/01/17Minneapolis, MNKansas City, MO9:55AM
DOE/JOHN325/01/1725/01/17Kansas City, MOMinneapolis, MN7:49PM
DOE/JOHN425/01/1725/01/17Minneapolis, MNDuluth, MN10:00PM
PICKLE/FARMER131/07/1731/07/17Minneapolis, MNKansas City, MO7:05AM
PICKLE/FARMER22/08/172/08/17Kansas City, MOMinneapolis, MN7:49PM
SMITH/MRS.131/03/1731/03/17Madison, WIMinneapolis, MN6:40AM
SMITH/MRS.231/03/1731/03/17Minneapolis, MNKansas City, MO9:55AM
SMITH/MRS.31/04/171/04/17Kansas City, MOMinneapolis, MN7:49PM
SMITH/MRS.41/04/171/04/17Minneapolis, MNMadison, WI10:20PM

<colgroup><col width="145" style="width: 109pt;"><col width="80" style="width: 60pt;"><col width="115" span="5" style="width: 86pt;"></colgroup><tbody>
</tbody>

Ideally what I would have would look like this...

DOE/JOHN16/08/176/08/17Duluth, MNMinneapolis, MN7:00AM26/08/176/08/17Minneapolis, MNKansas City, MO9:55AM37/08/177/08/17Kansas City, MOMinneapolis, MN7:49PM47/08/177/08/17Minneapolis, MNDuluth, MN10:00PM
PICKLE/FARMER131/07/1731/07/17Minneapolis, MNKansas City, MO7:05AM22/08/172/08/17Kansas City, MOMinneapolis, MN7:49PM
SMITH/MRS.131/07/1731/07/17Madison, WIMinneapolis, MN6:40AM231/07/1731/07/17Minneapolis, MNKansas City, MO9:55AM31/08/171/08/17Kansas City, MOMinneapolis, MN7:49PM41/08/171/08/17Minneapolis, MNMadison, WI10:20PM

<colgroup><col width="168" style="width:126pt"> <col width="20" style="width:15pt"> <col width="85" style="width:64pt" span="2"> <col width="149" style="width:112pt"> <col width="152" style="width:114pt"> <col width="75" style="width:56pt"> <col width="20" style="width:15pt"> <col width="85" style="width:64pt" span="2"> <col width="152" style="width:114pt" span="2"> <col width="75" style="width:56pt"> <col width="20" style="width:15pt"> <col width="74" style="width:56pt" span="2"> <col width="152" style="width:114pt"> <col width="149" style="width:112pt"> <col width="75" style="width:56pt"> <col width="20" style="width:15pt"> <col width="74" style="width:56pt" span="2"> <col width="149" style="width:112pt"> <col width="116" style="width:87pt"> <col width="86" style="width:65pt"> </colgroup><tbody>
</tbody>

I was thinking something along the lines of IF name field matches cell above cut cells 8 thru 13 of current row, offset up 1 row, find first empty cell to the right, then paste. IF does not match cell above, drop to cell in col A and repeat. Loop thru each row until end.

If you have anything idea of how to accomplish this, I'm good with that, but this was what my mind conceived.

Any help would be appreciated!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
RLCornish, try this code - assuming your current sheet structure is exactly the same as shown above. If you need any further help let me know

Code:
Sub test()

Dim PassName As String, Seq As Integer, LastCol As Single, LastRow As Single, OrgRg As Range, NewRg As Range

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = 7

For x = 2 To LastRow
    PassName = Cells(x, 1).Value
    Seq = ActiveSheet.Range("B" & x).Value
        If PassName = Cells(x, 1).Value And Seq > 1 Then
            Set OrgRg = Range("B" & x & ":G" & x)
            Set NewRg = Range(Cells(x - Seq + 1, (Seq * 6) - 4), Cells(x - Seq + 1, (Seq * 6) + 1))
                If ((Seq * 6) + 1) > LastCol Then LastCol = ((Seq * 6) + 1)
                NewRg.Value = OrgRg.Value
        End If
Next x

Set NewRg = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastCol))

NewRg.Sort Key1:=Range("B2:B" & LastRow), Order1:=xlAscending, Header:=xlYes

Dim FindFirst As Single
FindFirst = Application.WorksheetFunction.Match(2, Range("B2:B" & LastRow), 0) + 1

Rows(FindFirst & ":" & LastRow + 1).EntireRow.Delete

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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