How to copy data from one file and paste it into a dynamically named workbook

healeydm

New Member
Joined
Mar 5, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am currently creating a macro to improve a weekly file update process. I have to copy a specific range of data from one file (file name "Previous Weeks File") and paste it into my current week file - the new week's file name has date changes: "File Update - 03-17 (Weekly)". How can I copy the data from the "Previous Weeks File" and into the dynamically named workbook I'm updating?

I've tried to use the ThisWorkbook function, but it only works when it's being used in the original file I created the macro in. This macro will be used in a new file every week, so that function isn't a viable solution. I also plan to send this macro to the process owner once it's completed so they can use this to make their weekly update process more time efficient.

VBA Code:
Windows("Previous Weeks File.xlsx").Activate
    Sheets("Summary").Select
    Range("G7:G9").Select
    Selection.Copy
ThisWorkbook.Activate
    Sheets("Summary").Select
    Range("H7:H9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you're in the new workbook at the time you run the macro you can set a variable to that workbook
IE.
VBA Code:
Set Currentweek = Activeworkbook
Windows("Previous Weeks File.xlsx").Activate
Currentweek.Sheets("Summary").Range("H7:H9").value = Sheets("Summary").Range("G7:G9").Value

Set Currentweek = Nothing
if that doesn't work try this:
VBA Code:
Set Currentweek = Activeworkbook
Windows("Previous Weeks File.xlsx").Activate
    Sheets("Summary").Select
    Range("G7:G9").Select
    Selection.Copy
Currentweek.Activate
    Sheets("Summary").Select
    Range("H7:H9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Set Currentweek = Nothing
 
Upvote 1
Solution
If you're in the new workbook at the time you run the macro you can set a variable to that workbook
IE.
VBA Code:
Set Currentweek = Activeworkbook
Windows("Previous Weeks File.xlsx").Activate
Currentweek.Sheets("Summary").Range("H7:H9").value = Sheets("Summary").Range("G7:G9").Value

Set Currentweek = Nothing
if that doesn't work try this:
VBA Code:
Set Currentweek = Activeworkbook
Windows("Previous Weeks File.xlsx").Activate
    Sheets("Summary").Select
    Range("G7:G9").Select
    Selection.Copy
Currentweek.Activate
    Sheets("Summary").Select
    Range("H7:H9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Set Currentweek = Nothing
Thank you so much - both worked beautifully! The only issue I'm having is after copying from "Previous Weeks File" to the dynamically named file, the "Previous Weeks File" is now the active workbook and I'm not able to get back to the dynamic file and continue the macro. Any advice on how to correct this?
 
Upvote 0
Thank you so much - both worked beautifully! The only issue I'm having is after copying from "Previous Weeks File" to the dynamically named file, the "Previous Weeks File" is now the active workbook and I'm not able to get back to the dynamic file and continue the macro. Any advice on how to correct this?
VBA Code:
Set Currentweek = Activeworkbook
Windows("Previous Weeks File.xlsx").Activate
     Set previousweek = activeworkbook
    Sheets("Summary").Select
    Range("G7:G9").Select
    Selection.Copy
Currentweek.Activate
    Sheets("Summary").Select
    Range("H7:H9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
previousweek.activate
Set Currentweek = Nothing
Set previousweek = nothing
This will revert to previous week once the paste has occurred
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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