Cleaning up repetitive VBA

mssbass

Active Member
Joined
Nov 14, 2002
Messages
253
Platform
  1. Windows
I have several lines of the same code which copies from several different worksheets - is there a way to clean this up a little?

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("Auto Provider")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("A3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("Future")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("D3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("Sleep - Attached")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("G3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("CGM")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("J3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("Diabetic Supplies")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("M3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("Breast Pumps")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("P3")

Set wsCopy = Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx").Worksheets("FL Blue Medicare")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wsCopy.Range("H2:H" & lCopyLastRow).Copy _
wsDest.Range("S3")
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
Code:
   Dim Ary As Variant
   
   Ary = Array("Auto Provider", "A3", "Future", "D3", "Sleep - Attached", "G3")
   
   For i = 0 To UBound(Ary) Step 2
      With Workbooks("Staffing_Routing_Point_Open_Report_" & txtDate & ".xlsx")
         With .Sheets(Ary(i))
            .Range("H2:H" & .Range("A" & Rows.Count).End(xlUp).Row).Copy wsDest.Range(Ary(i + 1))
         End With
      End With
   Next i
 
Upvote 0
If it is ALL the sheets in your workbook, it is even easier. You can simply loop through all sheets (without having to store their names in an array). Something like:
Code:
Dim ws as Worksheet
For Each ws in Worksheets
    With ws
        'your code here
    End With
Next ws
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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