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

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

All Courses

From novice to tech pro — start learning today.