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.
Sample-loop-file.xlsm
LVL 11
RayData AnalystAsked:
Who is Participating?
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 = ""
            Else
                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..

Saurabh...
Sample-loop-file.xlsm
0

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
    
    Columns("A:A").Select
    Selection.AutoFilter
    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
    
    Columns("C:C").Select
    Selection.AutoFilter
    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
    
    Selection.AutoFilter
End Sub

Open in new window

0
Hakan YılmazTechnical Office MEP 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

0
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!
0
Saurabh Singh TeotiaCommented:
Thanks Ray.. Glad it worked for you.. :-)

Saurabh...
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.