Update a macro with new name of wkbook

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
I create a new workbook each day (titled as PREV.DAY'SDATE) by copying the previous day's workbook and changing the file name. I recorded a macro in today's workbook, and everything about it will work except all the mentions of the filename. How can I change all mentions of "9Aug11.xls" to the current filename, be that "10Aug11.xls", or later "8Sept11.xls", etc.?

Here's a little sample of code to change:
Code:
    Windows("text1.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Windows("9Aug11.xls").Activate
    Range("B5").Select
    ActiveSheet.Paste
    Windows("text2.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("9Aug11.xls").Activate
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Dim wb As Workbook
Set wb = Activeworkbook
Windows("text1.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    wb.Activate
    Range("B5").Select
    ActiveSheet.Paste
    Windows("text2.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.Copy
    wb.Activate
 
Upvote 0
There may be a problem with that: each of those .txt files? Those have actually just been copied into excel (I just haven't saved them because they only exist temporarily). So I think ActiveWorkbook would not always point to the file I want...

I think what I'm looking for (if it exists) is for the macro to look at the filename, and replace "9Aug11.xls" with whatever that is (13Sept11.xls...5Dec11.xls...etc).
 
Last edited:
Upvote 0
Code:
Dim fn As String
fn = Application.GetOpenFilename
fn = Dir(fn)
Windows("text1.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Windows(fn).Activate
    Range("B5").Select
    ActiveSheet.Paste
    Windows("text2.txt").Activate
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows(fn).Activate

There is no validation in this so I you hit Cancel in the File Select screen the MAcro will die :)
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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