Simplify/shorten a macro

EOAEvan

Active Member
Joined
Sep 20, 2007
Messages
399
Is there a way to shorten this? The end result will be multiple workbooks/sheets/ranges and the macro would be hundreds of lines long if kept this way. For this example alone the actual version is 4x longer than this. Its a exact copy of this but changes the cells it copies from and the destination file. Any suggestions would be appreciated.

Code:
Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("M4:M106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("J4:J106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("N4:N106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("L4:L106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("O4:O106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("R4:R106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("P4:P106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("T4:T106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("Q4:Q106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("Z4:Z106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("R4:R106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("AB4:AB106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("S4:S106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("AH4:AH106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("T4:T106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("AJ4:AJ106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("U4:U106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("AP4:AP106")
    Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col").Range("V4:V106").Copy
        ActiveSheet.Paste Destination:=Workbooks("Collections - Central").Sheets("Feb 09").Range("AR4:AR106")
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
one way that will make a lot less typing and make the code much more readable is to set objects for the source and destination sheets
set ssht = Workbooks("Account Lookup Table - Dept ROF File Output").Sheets("ROF Col")
set dsht =Workbooks("Collections - Central").Sheets("Feb 09")

you can also use with blocks to make the code more manageable again
Code:
with ssht
    .range("M4:M106").Copy dsht.range("J4:J106")
    ' all your ranges here


end with

to be able to use a loop to copy all the ranges, you would need to have some sort of table of the ranges (columns) to copy and the destination columns, this could be in an excel sheet or textfile, to copy more ranges just add the source and destination to the list, code stays the same
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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