Problem with renameing a doc with a macro...

rogersj

Board Regular
Joined
Aug 13, 2002
Messages
187
I have a macro that flips back and forth coping and pasting information. But if someone renames document A, The macro wont work because it's looking for a certain document name. How can I fix this? Thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,499
Office Version
  1. 365
Platform
  1. Windows
You didn't say if the files should have the same name all the time, or if the user is prompted to choose the file when opening it.

Regardless, I have a few suggestions which may or may not help you. The first, is to prompt the user to select the file(s) to copy to/from. Then the macro can run off the file names which are selected.

If there is a danger of the file name being changed while the macro is running, I would recommend including code that makes a copy of the files(s) when you open them (i.e. save them as TempFileA and TempFileB). The run the macros off of these copies.
 

rogersj

Board Regular
Joined
Aug 13, 2002
Messages
187
If they download the excel macro off the website, or better yet, if they just open it from there, it will save on the HDD as some crazy temp. name, and the macro wont run correctly because that file needs to be named a certain file name.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,499
Office Version
  1. 365
Platform
  1. Windows
I think you may need to explain a little more how exactly this program works. When you talk about the name of the file being changed, are you referring to the file with the macro or the data files? How many files are involved? How are the other files opened?

Regardless, you should be able to program the macro to copy the files (including the file containing the macro itself) to temp files in the C: drive. Basically, you are just renaming all the files involved so you will know there exact name and can invoke them.
 

rogersj

Board Regular
Joined
Aug 13, 2002
Messages
187

ADVERTISEMENT

Ok, What the macro does, it copies data from one workbook to another. The macro itself is within Workbook1.xls. Now, my macro is set up to open Workbook2, flip back to Workbook1, Copy data, flip over to Workbook2 and paste. If one of my co workers rename Workbook1... The macro cant flip back to Workbook1.xls during the macro because that file has a new name.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,499
Office Version
  1. 365
Platform
  1. Windows
OK, I did some research and found a better way. Use the following code to "capture" the file name, whatever it may be:

myName = ThisWorkbook.Name

You obviously want to do this in the macro before it opens the other file. Then, when we want to go back to this file, simply use the following code:

Windows(myName).Activate

This should solve the problem.
 

Forum statistics

Threads
1,144,329
Messages
5,723,733
Members
422,512
Latest member
MHau5

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
Top