Linked spreadsheet filepath query r.e. saving?

pangster

Board Regular
Joined
Jun 15, 2005
Messages
160
Hi all,

I have a quick question that i'm hoping some of you guys can help me with.

I have a target workbook which has 3 cells on the first sheet which detail:

the filepath (i.e.: \\Desktop\Action Plan Trackers) = H13
the spreadsheet name (i.e.: MAIN Tracker v1.3.xls)= H14
and the sheet name (i.e.: Business Case) = H15

At the moment this works well and it imports the data from the source workbook. Ultimately i want to use the target workbook for more than one source (not at the same time) - (the target workbook prompts to be saved after the import has been completed) and wanted to know if there was an easy way of automatically updating the filepath in the target workbook with the details from the source workbook

I kind of had an idea that the agent completes the data in the source workbooks - triggers a macro off of a command button i.e. Export - this then opens the Target workbook (which updates H13, H14 and H15) with the details of the source workbook automatically, the import macro then completes and the user is prompted to save the workbook with the correct name...

I'm not sure how easy/hard this would be to achieve though? - so any help would be fantastic!!!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
it sounds fairly simple, if I understand your question. Apologies if I don't!

All you would need is to make the command button run a sub in the target file (so you create the VBA in target, put a button in source and assign the target VBA sub to the button.

Then you could just pick up the 3 cells of data from the source and use workbooks(target).sheet(sheetname).cells(13,8).value = data (H13).

the sourcebook name would be picked up by activeworkbook.name whilst the source file was still active (i.e. before activating target) - you need to do that first or you'd have problems if the agent had multiple workbooks open.

If you know the name it should be saved as, don't prompt the agent to save it - create the path and filename and save it within the code. Giving a human agent the opportunities to save it with the wrong name will just mean that people occasionally save it with the wrong name.
 
Upvote 0
it sounds fairly simple, if I understand your question. Apologies if I don't!

All you would need is to make the command button run a sub in the target file (so you create the VBA in target, put a button in source and assign the target VBA sub to the button.

Then you could just pick up the 3 cells of data from the source and use workbooks(target).sheet(sheetname).cells(13,8).value = data (H13).

the sourcebook name would be picked up by activeworkbook.name whilst the source file was still active (i.e. before activating target) - you need to do that first or you'd have problems if the agent had multiple workbooks open.

If you know the name it should be saved as, don't prompt the agent to save it - create the path and filename and save it within the code. Giving a human agent the opportunities to save it with the wrong name will just mean that people occasionally save it with the wrong name.

thanks for the reply Johnny.. how would the code look for this?

i guess in step wise format what i'm looking for is:
1. agent presses Export button on Business Case sheet
2. Business Case (target workbook) is opened
3. file location details for source workbook are updated on H13, H14 and H15
4. import macro is executed
5. Once import macro completes - agent is prompted to save file

PS i know what you mean about agent behaviour etc but this will be covering probably 20+ initiatives all with different names/versions so not really feasible to pre-populate

thanks again for your help - do you have any sample code i could look over/adapt as i'm still finding my feet with VB?
 
Upvote 0
Okay.

What you really need to do is create a macro file. The name of this needs to be specified in the macro, as you need to get the macro to close the macro file when the export is done. I tried putting the macro into the target file and that made it chuck a wobbler, opening the file interrupted the macro. So you need a separate macro file. Attach the macro to a button on the toolbar, it will open the macro file whilst the macro runs but the macro closes itself using the name specified in the macro - so make sure they are the same

The macro (in the macro file) creates a blank file (or you can open an existing template)

The copying names to cells is easy.

The hard bit comes in saving the file. Now it may be the weird setup I have here (our IT dept puts all sorts of dodgy Office patches in) but I can't get it to prompt for a filename, and I'm sure I've done it before. The macro saves a blank book as <blankbook.xls> in the current folder. I tried saveas, I tried savecopyas. I tried opening a template sheet as readonly and saving it to try and open a conflict resolution box but that crashed it. Maybe someone else can help, maybe it's just my setup, so try it yourself. If you can't force it to show a save as dialog, you'll have to either use an inputbox to get the user to enter a filename, directory etc, or else generate it yourself.

Code:
macrofile = "macro.xls"
sourcefilename = ActiveWorkbook.Name
sourcepath = ActiveWorkbook.Path
sourcesheet = ActiveSheet.Name

then either
Code:
Workbooks.Add
or
Code:
Workbooks.Open Filename:="C:\Book12.xls"

the next few lines put the data you want into the cells you want (H13:15)
Code:
targetfile = ActiveWorkbook.Name

Workbooks(targetfile).Activate
Cells(13, 8).Value = sourcepath
Cells(14, 8).Value = sourcefilename
Cells(15, 8).Value = sourcesheet

'     call import routine

Workbooks(targetfile).Activate
Workbooks(targetfile).Save
Workbooks(macrofile).Close

Hope this helps
 
Last edited:
Upvote 0
Okay.

What you really need to do is create a macro file. The name of this needs to be specified in the macro, as you need to get the macro to close the macro file when the export is done. I tried putting the macro into the target file and that made it chuck a wobbler, opening the file interrupted the macro. So you need a separate macro file. Attach the macro to a button on the toolbar, it will open the macro file whilst the macro runs but the macro closes itself using the name specified in the macro - so make sure they are the same

The macro (in the macro file) creates a blank file (or you can open an existing template)

The copying names to cells is easy.

The hard bit comes in saving the file. Now it may be the weird setup I have here (our IT dept puts all sorts of dodgy Office patches in) but I can't get it to prompt for a filename, and I'm sure I've done it before. The macro saves a blank book as <blankbook.xls> in the current folder. I tried saveas, I tried savecopyas. I tried opening a template sheet as readonly and saving it to try and open a conflict resolution box but that crashed it. Maybe someone else can help, maybe it's just my setup, so try it yourself. If you can't force it to show a save as dialog, you'll have to either use an inputbox to get the user to enter a filename, directory etc, or else generate it yourself.

Code:
macrofile = "macro.xls"
sourcefilename = ActiveWorkbook.Name
sourcepath = ActiveWorkbook.Path
sourcesheet = ActiveSheet.Name

then either
Code:
Workbooks.Add
or
Code:
Workbooks.Open Filename:="C:\Book12.xls"

the next few lines put the data you want into the cells you want (H13:15)
Code:
targetfile = ActiveWorkbook.Name

Workbooks(targetfile).Activate
Cells(13, 8).Value = sourcepath
Cells(14, 8).Value = sourcefilename
Cells(15, 8).Value = sourcesheet

'     call import routine

Workbooks(targetfile).Activate
Workbooks(targetfile).Save
Workbooks(macrofile).Close

Hope this helps

Johnny - this is fantastic stuff - i'll give it a shot when i'm back in the office, but i see now which components i need to use so hopefully it'll just be a case of tinkering about until i get it working..

if i manage to get it working successfully i'll post the code incase anyone else needs it in future..

thanks again for your help and replies and for getting the old cogs ticking!
 
Upvote 0
ok - i've been playing around with my code now and i think i'm getting somewhere.. although i need HELP! lol! - here's where i am just now:

Sub Button_BUSINESS_CASE_EXPORT()

'This code copies the open workbook details (source workbook) to cells C84 to C86 in the source Business Case sheet
Sheets("Business Case").Select
Range("C84") = ActiveWorkbook.Path
Range("C85") = ActiveWorkbook.Name
Range("C86") = ActiveSheet.Name

'This code opens up the Business Case Template file
myDir = "C:\Documents and Settings\JOE BLOGGS\Desktop\ACTION PLANS"
CreateObject("WScript.Shell").CurrentDirectory = myDir

' update filename here
Workbooks.Open Filename:=myDir + "\Business Case template v1.0.xls"

OrigWB = Sheets("Business Case").Range("C85").Value
OrigWS = Sheets("Business Case").Range("C86").Value

TargWB = "Business Case template v1.0.xls"
TargWS = "Executive Summary"

Workbooks(TargWB).Sheets(TargWS).Range("H13:H15").ClearContents

Workbooks(OrigWB).Sheets(OrigWS).Range("C84").Copy Workbooks(TargWB).Sheets(TargWS).Range("H13")
Workbooks(OrigWB).Sheets(OrigWS).Range("C85").Copy Workbooks(TargWB).Sheets(TargWS).Range("H14")
Workbooks(OrigWB).Sheets(OrigWS).Range("C86").Copy Workbooks(TargWB).Sheets(TargWS).Range("H15")

Workbooks(TargWB).Sheets(TargWS).Select

Call GetValue_ExecutiveSummaryUpdate

End Sub

The above code copies the source details (filepath, filename and sheet name) into 3 cells on the source workbook, these 3 values are then copied into the correct cells on the Target workbook but i'm having problems when i try and run the update macro - (this is called GetValue_ExecutiveSummaryUpdate) it comes up with a Sub or Function not defined error?? - please can anyone advise on a way around this??

thanks for any help offered!! if you can suggest improvements to the code i've written i'm also open to suggestions!! :)
 
Upvote 0
Hi again
I guess you're in a different timezone, US? would have replied sooner

First thought, and this sounds really stupid, is the name of the proc you're calling GetValue_ExecutiveSummaryUpdate
spelt correctly in the sub GetValue_ExecutiveSummaryUpdate declaration?

I copied your code, commented out the bits which wouldn't work without the files, and got the same error for exactly that reason :)

The only other thing I can think of is, where is the GetValue_ExecutiveSummaryUpdate macro? In the same module? If not, try it in there
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

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