Macro to Copy and Paste Values to File Based on Tab Name

ksreed

New Member
Joined
Feb 11, 2010
Messages
25
Hello,

I need to copy and paste a set of values from one file to another file based on tab name.

For instance, if I have a tab named "Sales," I would need to copy data in B1:B10 into the file in X:\USA\Regional\Sales.xls in D20:29.

Is there a way to reference the tab name to open a file and copy and paste the values into that file (I need to do this for about 20 or so tabs)?

Note: The tabs and files' names match one for one so I just need to add a directory reference somewhere in the macro.

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I have this code that needs to be repeated for 20 or so tabs.

1. What Loop or For statement do I need to use to move through all of the tabs in the "State Budget East.xls" file?

2. Is there a shortcut to cut down on the amount of code?

I would use this macro from the "State Budget East" file being open.

Thank you for any help.

Code:
    Workbooks.Open Filename:= _
    "X:\East\North Carolina\2011 North Carolina State budget.xls"
    Windows("State Budget East.xls").Activate
    Sheets("North Carolina").Select
    Range("G5:G16").Select
    Selection.Copy
    Windows("2011 North Carolina State budget.xls").Activate
    Sheets("Expenses").Select
    ActiveWindow.SmallScroll Down:=162
    Range("H196").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    ActiveWorkbook.Save
    ActiveWindow.Close
 
    Workbooks.Open Filename:= _
    "X:\East\Virginia\2011 Virginia State budget.xls"
    Windows("State Budget East.xls").Activate
    Sheets("Virginia").Select
    Range("G5:G16").Select
    Selection.Copy
    Windows("2011 Virginia State budget.xls").Activate
    Sheets("Expenses").Select
    ActiveWindow.SmallScroll Down:=162
    Range("H196").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    ActiveWorkbook.Save
    ActiveWindow.Close
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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