Have Excel VBA move data over 1 or 2 columns based on values - Ultimately get data aligned

I've attached a sample workbook with data like I have and what I want.  I need VBA code to transfer data accordingly.

I'm pretty sure I need a loop that will run through the first column (A) and find each row that is NOT "delivery".  If not "delivery" then move col A & B over to Col C & D.  Then do the same concept on Column C where each row that is not "beverage".  If not "beverage", then move col C&D to E&F.  

I know it likely sounds confusing, but I'm fairly certain when you see the sample workbook it will be crystal clear.

Thanks in advance.
LVL 11
RayData AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following code which will do what you are looking for...

Sub arrangedata()
    Dim rng As Range, cell As Range
    Dim lrow As Long

    lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = Range("A2:A" & lrow)

    For Each cell In rng

        If InStr(1, cell.Value, "delivery", vbTextCompare) = 0 Then

            If InStr(1, cell.Value, "beverage", vbTextCompare) = 0 Then
                Cells(cell.Row, "e").Value = cell.Value
                cell.Value = ""
                Cells(cell.Row, "f").Value = cell.Offset(0, 1).Value
                cell.Offset(0, 1).Value = ""
                Cells(cell.Row, "c").Value = cell.Value
                cell.Value = ""
                Cells(cell.Row, "d").Value = cell.Offset(0, 1).Value
                cell.Offset(0, 1).Value = ""
            End If
        End If

    Next cell

End Sub

Open in new window

Enclosed is your workbook for your reference where i have run this code at the sheet2..


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
No looping required

Sub MoveData()
    Dim lngLastRow As Long

    lngLastRow = Range("A1048576").End(xlUp).Row
    ActiveSheet.Range("A1:A" & lngLastRow).AutoFilter Field:=1, Criteria1:="<>delivery"
    Range("A1:B" & lngLastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=Range("C1:D" & lngLastRow)
    Range("A1:B" & lngLastRow).SpecialCells(xlCellTypeVisible).ClearContents
    ActiveSheet.Range("C1:C" & lngLastRow).AutoFilter Field:=1, Criteria1:="<>beverage"
    Range("C1:D" & lngLastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=Range("E1:F" & lngLastRow)
    Range("C1:D" & lngLastRow).SpecialCells(xlCellTypeVisible).ClearContents
End Sub

Open in new window

Hakan YılmazProject EngineerCommented:
Another basic loop solution.
Sub hakan()
    Dim i As Integer
    For i = 2 To Sheet1.UsedRange.Rows.Count
        If Cells(i, 1).Value <> "delivery" Then
            Cells(i, 3).Value = Cells(i, 1).Value
            Cells(i, 4).Value = Cells(i, 2).Value
            Cells(i, 1).Value = ""
            Cells(i, 2).Value = ""
        End If
        If Cells(i, 3).Value <> "beverage" Then
            Cells(i, 5).Value = Cells(i, 3).Value
            Cells(i, 6).Value = Cells(i, 4).Value
            Cells(i, 3).Value = ""
            Cells(i, 4).Value = ""
        End If
    Next i
End Sub

Open in new window

RayData AnalystAuthor Commented:
Ok, it seems as though all of them worked on my sample workbook just fine.  Ultimately, as is typical, the end project did require some adaptation and Saurabh's solution leant itself to adapting quite well and it was first.  I will also freely admit that I've 'followed' Saurabh for some time and was therefore slightly biased toward using his solution.  Therefore, most points to Saurabh and partial credit for Martin and Hakan.

Excellent work by all of you and speedy responses to my hastily thrown together sample (was about to leave work for the weekend).

Thank you all!
Saurabh Singh TeotiaCommented:
Thanks Ray.. Glad it worked for you.. :-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.