|
-
Jun 18th, 2012, 08:22 AM
#1
Thread Starter
Junior Member
[RESOLVED] Browse for workbook, set path as variable
I have an activex button that browses for a workbook and sets the path as a string (myFilePath).
In another sub I dim DD as worksheet, and try to set it as myFilePath like so:
Code:
Set DD = Workbooks(myFilePath).Worksheets("2012")
Any idea why this does not work and/or how I can make it work?
Let me know if this isnt clear. Thanks!
-
Jun 18th, 2012, 10:17 AM
#2
Re: Browse for workbook, set path as variable
What exactly is stored in the "myFilePath" variable?
From MS:
"Use Workbooks(index), where index is the workbook name or index number, to return a single Workbook object."
-
Jun 18th, 2012, 01:23 PM
#3
Thread Starter
Junior Member
Re: Browse for workbook, set path as variable
I actually figured it out. thank you though.
-
Sep 10th, 2012, 05:13 AM
#4
New Member
Re: [RESOLVED] Browse for workbook, set path as variable
hi bheltzel
I have exactly the same problem... can you share how you solved this?
thanks!
Sir
-
Sep 10th, 2012, 05:32 AM
#5
Re: [RESOLVED] Browse for workbook, set path as variable
You have to use the Add-method of the Workbooks-Collection first to add the Workbook you're trying to access.
Or you can use the Open-Method of the Workbook-Object.
-
Sep 10th, 2012, 06:42 AM
#6
New Member
Re: [RESOLVED] Browse for workbook, set path as variable
 Originally Posted by Zvoni
You have to use the Add-method of the Workbooks-Collection first to add the Workbook you're trying to access.
Or you can use the Open-Method of the Workbook-Object.
Is there a method to do this without opening the file? I am using this for several files inside an IF cycle... moreover the files are on a network folder...
By the way, I tried this:
Code:
Workbooks.Add (Path)
Set wb = Workbooks(Path).Worksheets(SheetName)
but I get "runtime error '9' - subscript out of range", which is the same error I got before adding "workbooks.add"
what am I missing?
Last edited by sirjoem; Sep 10th, 2012 at 07:19 AM.
-
Sep 10th, 2012, 07:22 AM
#7
Re: [RESOLVED] Browse for workbook, set path as variable
Workbooks(Path) is wrong.
You have to use either the numerical Index of your Workbook, or the Name of your Workbook.
Try the following:
Code:
For Each wb in Workbooks
Debug.Print wb.name
Next
You'll see what i mean
EDIT: The Add-Method is BullS***. Sorry.
The Add-Method is for adding a new blank Workbook based on a template.
The Open-Method is the Correct one
-
Sep 11th, 2012, 06:28 AM
#8
New Member
Re: [RESOLVED] Browse for workbook, set path as variable
 Originally Posted by Zvoni
Workbooks(Path) is wrong.
You have to use either the numerical Index of your Workbook, or the Name of your Workbook.
this works now! thanks!
Sir
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|