Pass macro to another workbook

BrendanDixon

Board Regular
Joined
Mar 7, 2010
Messages
174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All,

I hope someone can help me here. I am creating a macro to update the workbooks of users. What will happen is
1. The user will have a workbook opened in folder other their choice, but usually their desktop.
2. when the opened workbook detects there is a newer version.
3. The opened workbook will be renamed as a temporary file in the same folder.
4. It will then delete the original workbook from the folder and then copy the new workbook from the server to the original files directory.
5. It will then copy certain data that the user had in their original workbook which is now the temporary file to the new workbook.
6. I then want to close the temporary file and delete it. This is the tricky part

I did manage to have some code to do this. but it would close the workbook and delete it but would leave excel open and the user would have to close excel and then open the workbook again. which I thought was not ideal

I then came across this method of Application.OnTime Now(), "'TheNewWorkbook.xlsm'!Macro"
Where TheNewWorkbook is the updated workbook in the users folder that the temporary workbook will pass the macro to, and
Macro is the macro in the new workbook that will close and delete the temporary file.

This system worked perfectly in my test code. The problem I have is that the users may rename the workbook to what they think it should be. so the 'TheNewWorkbook.xlsm' needs to be dynamic. I had the following

VBA Code:
Dim Name as string

Name = ThisWorkbook.Name ' This is set while original workbook is opened

Application.OnTime Now(), "'Name'!Macro"

The problem is that when it gets to Application.OnTime Now(), "'Name'!Macro"
It comes up with an error that it "cannot find "C:\Users\abc\Documents\Name.xlsx" even though it is on the desktop with xlsm extension
and then "cannot open Name.htm"


Can anyone tell me what is happening here?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Not for sure but you could change ThisWorkbook.Name in
VBA Code:
ThisWorkbook.FullName
and see what happens ....
 
Upvote 0
Hi WWteB,

I have already tried that. it will then come up with the error "cannot find "C:\Users\abc\Documents\C:\Users\abc\Desktop\Name.xlsx"
and "cannot open C:\Users\abc\Desktop\Name.htm"
 
Upvote 0
I overlooked this part
I did manage to have some code to do this. but it would close the workbook and delete it but would leave excel open and the user would have to close excel and then open the workbook again. which I thought was not ideal
I suspect that you also close the workbook where your code runs. That could possibly cause that error. If so, it might be wise to have a separate workbook with your code from which you direct everything that needs to be done. Excel therefore does not have to be closed by the user.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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