VBA - Count look cleanup

Hi ,

I have this below macro that count loops based on number of rows with data in my Consolidation sheet.

I'm trying to decrease the size of the below code so that it count the number of rows and use the proper case automatically.

It is hard to send an excel template for now, due to confidential data inside but if really needed, i can try to replicate this into a template.

Thanks for your help.

 Select Case Countloop
                    
                    Case 1
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2")
                    
                    Case 2
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3")
                    
                    Case 3
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4")

                    Case 4
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5")
                    
                    Case 5
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5") & _
                    vbNewLine & WS2.Range("J6") & vbTab & WS2.Range("K6") & vbTab & WS2.Range("L6") & buffer & WS2.Range("M6") & vbTab & WS2.Range("F6") & vbTab & WS2.Range("G6")
                    
                    Case 6
                    
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5") & _
                    vbNewLine & WS2.Range("J6") & vbTab & WS2.Range("K6") & vbTab & WS2.Range("L6") & buffer & WS2.Range("M6") & vbTab & WS2.Range("F6") & vbTab & WS2.Range("G6") & _
                    vbNewLine & WS2.Range("J7") & vbTab & WS2.Range("K7") & vbTab & WS2.Range("L7") & buffer & WS2.Range("M7") & vbTab & WS2.Range("F7") & vbTab & WS2.Range("G7")
                    
                    Case 7
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5") & _
                    vbNewLine & WS2.Range("J6") & vbTab & WS2.Range("K6") & vbTab & WS2.Range("L6") & buffer & WS2.Range("M6") & vbTab & WS2.Range("F6") & vbTab & WS2.Range("G6") & _
                    vbNewLine & WS2.Range("J7") & vbTab & WS2.Range("K7") & vbTab & WS2.Range("L7") & buffer & WS2.Range("M7") & vbTab & WS2.Range("F7") & vbTab & WS2.Range("G7") & _
                    vbNewLine & WS2.Range("J8") & vbTab & WS2.Range("K8") & vbTab & WS2.Range("L8") & buffer & WS2.Range("M8") & vbTab & WS2.Range("F8") & vbTab & WS2.Range("G8")
                    
                    Case 8
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5") & _
                    vbNewLine & WS2.Range("J6") & vbTab & WS2.Range("K6") & vbTab & WS2.Range("L6") & buffer & WS2.Range("M6") & vbTab & WS2.Range("F6") & vbTab & WS2.Range("G6") & _
                    vbNewLine & WS2.Range("J7") & vbTab & WS2.Range("K7") & vbTab & WS2.Range("L7") & buffer & WS2.Range("M7") & vbTab & WS2.Range("F7") & vbTab & WS2.Range("G7") & _
                    vbNewLine & WS2.Range("J8") & vbTab & WS2.Range("K8") & vbTab & WS2.Range("L8") & buffer & WS2.Range("M8") & vbTab & WS2.Range("F8") & vbTab & WS2.Range("G8") & _
                    vbNewLine & WS2.Range("J9") & vbTab & WS2.Range("K9") & vbTab & WS2.Range("L9") & buffer & WS2.Range("M9") & vbTab & WS2.Range("F9") & vbTab & WS2.Range("G9")
                    
                    Case 9
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    vbNewLine & WS2.Range("J2") & vbTab & WS2.Range("K2") & vbTab & WS2.Range("L2") & buffer & WS2.Range("M2") & vbTab & WS2.Range("F2") & vbTab & WS2.Range("G2") & _
                    vbNewLine & WS2.Range("J3") & vbTab & WS2.Range("K3") & vbTab & WS2.Range("L3") & buffer & WS2.Range("M3") & vbTab & WS2.Range("F3") & vbTab & WS2.Range("G3") & _
                    vbNewLine & WS2.Range("J4") & vbTab & WS2.Range("K4") & vbTab & WS2.Range("L4") & buffer & WS2.Range("M4") & vbTab & WS2.Range("F4") & vbTab & WS2.Range("G4") & _
                    vbNewLine & WS2.Range("J5") & vbTab & WS2.Range("K5") & vbTab & WS2.Range("L5") & buffer & WS2.Range("M5") & vbTab & WS2.Range("F5") & vbTab & WS2.Range("G5") & _
                    vbNewLine & WS2.Range("J6") & vbTab & WS2.Range("K6") & vbTab & WS2.Range("L6") & buffer & WS2.Range("M6") & vbTab & WS2.Range("F6") & vbTab & WS2.Range("G6") & _
                    vbNewLine & WS2.Range("J7") & vbTab & WS2.Range("K7") & vbTab & WS2.Range("L7") & buffer & WS2.Range("M7") & vbTab & WS2.Range("F7") & vbTab & WS2.Range("G7") & _
                    vbNewLine & WS2.Range("J8") & vbTab & WS2.Range("K8") & vbTab & WS2.Range("L8") & buffer & WS2.Range("M8") & vbTab & WS2.Range("F8") & vbTab & WS2.Range("G8") & _
                    vbNewLine & WS2.Range("J9") & vbTab & WS2.Range("K9") & vbTab & WS2.Range("L9") & buffer & WS2.Range("M9") & vbTab & WS2.Range("F9") & vbTab & WS2.Range("G9") & _
                    vbNewLine & WS2.Range("J10") & vbTab & WS2.Range("K10") & vbTab & WS2.Range("L10") & buffer & WS2.Range("M10") & vbTab & WS2.Range("F10") & vbTab & WS2.Range("G10")
                    

                    End Select

Open in new window

LVL 11
Wilder1626Asked:
Who is Participating?

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

x
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.

EdwardCommented:
Is this looping case.   Runs Case 1 then Case 2, case 3, etc?    Or is it getting the count 1st then choosing which case statement?


I'm seeing 1st off,  the 1st 3 lines are repeated.    Those you can just separate those out.  Run them 1st.
0
Rgonzo1971Commented:
Hi,

pls try

                    strTemp = ""
                    For Idx = 1 To Countloop
                        strTemp = strTemp & vbNewLine & WS2.Range("J" & Idx + 1) & vbTab & WS2.Range("K" & Idx + 1) & vbTab & WS2.Range("L" & Idx + 1) & buffer & WS2.Range("M" & Idx + 1) & vbTab & WS2.Range("F" & Idx + 1) & vbTab & WS2.Range("G" & Idx + 1)
                    Next
                    .Body = "  Driver: " & ws.Range("AE" & Target.Row) & buffer & "  Tractor: " & ws.Range("AD" & Target.Row) & buffer & "Planned Equipment: " & WS2.Range("B2") & _
                    vbNewLine & "Planned Depart " & WS2.Range("Y2") & buffer & "Actual Depart " & WS2.Range("Z2") & vbNewLine & _
                    strTemp

Open in new window

Regards
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
Wilder1626Author Commented:
This is exactly what i needed

Thank you so much for your help.
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.