Referencing workbooks opened with GetOpenFilename

AlexCS

Board Regular
Joined
Oct 31, 2011
Messages
78
Hi everyone,

I cannot seem to be able to work with the GetOpenFilename on the following piece of code (PLbook is the workbook containing the macro)

Dim CApath As Variant
Dim PLbook As Variant

ThisWorkbook.Save
CApath = Application.GetOpenFilename(Title:="Close Allocation Model")
Set wbOpen = Workbooks.Open(CApath)
PLbook = Application.GetOpenFilename(Title:="Commercial Packed P&L")
ThisWorkbook.Save
Set wb2Open = Workbooks.Open(PLbook)

Workbooks(wb2Open).Sheets("Act Plant Exp").Cells(6, 2).Value = Workbooks(wbOpen).Sheets("Bag in Box").Cells(52, 14).Value

If I use wb2Open and wbOpen the code does not crash but it does not do what it is meant to do either, namely attribute the value of a cell in the CApath workbook to another cell in the PLbook.

If I try to use CApath and PLbook instead of wbOpen I get a Subscript out of range error.

If I open the files prior to running the macro and mention their name via Input box the code works but unfortunately my users find it too difficult.

Please lend a hand, I have seen my code crash in every possible way since working on this!

Many thanks,

Alex
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Alex within you code are you refering to the file extension as I cannot see them?

Perhaps this will help.

Dim CApath As Variant
Dim PLbook As Variant

ThisWorkbook.Save
CApath = Application.GetOpenFilename(Title:="Close Allocation Model")
Set wbOpen = Workbooks.Open(CApath) & ".xls"
PLbook = Application.GetOpenFilename(Title:="Commercial Packed P&L")
ThisWorkbook.Save
Set wb2Open = Workbooks.Open(PLbook) & ".xls"
 
Upvote 0
Hi Trevor,
Thank you for your suggestion. The file extensions are included in the variables CApath and PLbook as part of GetOpenFilename and if I try to add them to wbOpen and wb2Open I get Run-time error 436 Object does not support property or method (I cannot even Msgbox wbOpen and wb2Open as they have not been declared and will prompt an error if declared as strings)
In the end, my problem is reduced to this:
If I use wbOpen and wb2Open as my document paths nothing will happen whatsoever afer running the below formula
Workbooks(wb2Open).Sheets("Act Plant Exp").Cells(6, 3).Value = Workbooks(wbOpen).Sheets("Bag in Box").Cells(53, 14).Value<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
If I use PLbook and CApath as my document paths I get Run-time error – Incorrect function when running the below piece of code
Workbooks(PLbook).Sheets("Act Plant Exp").Cells(6, 3).Value = Workbooks(CApath).Sheets("Bag in Box").Cells(53, 14).Value<o:p></o:p>
Is there any other way of referencing my two workbooks so that the formula actually works?
Kind regards,
Alex
<o:p> </o:p>
 
Upvote 0
Alex try this out and see what the code states.

Record a macro to open the 2 books and create the formula then stop recording, that will give you the basic code and then it can be adjusted.
 
Upvote 0
Hi Alex

You have references to the workbooks so you can use these to refer to the sheets - you do not wrap with Workbooks() ie:

Replace
Code:
Workbooks(wb2Open).Sheets("Act Plant Exp").Cells(6, 2).Value = Workbooks(wbOpen).Sheets("Bag in Box").Cells(52, 14).Value

With:

Code:
wb2Open.Sheets("Act Plant Exp").Cells(6, 2).Value = wbOpen.Sheets("Bag in Box").Cells(52, 14).Value
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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