Results 1 to 8 of 8

Thread: [RESOLVED] Browse for workbook, set path as variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Resolved [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!

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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."

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    24

    Re: Browse for workbook, set path as variable

    I actually figured it out. thank you though.

  4. #4
    New Member
    Join Date
    Sep 2012
    Posts
    3

    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

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    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.

  6. #6
    New Member
    Join Date
    Sep 2012
    Posts
    3

    Re: [RESOLVED] Browse for workbook, set path as variable

    Quote Originally Posted by Zvoni View Post
    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.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    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

  8. #8
    New Member
    Join Date
    Sep 2012
    Posts
    3

    Re: [RESOLVED] Browse for workbook, set path as variable

    Quote Originally Posted by Zvoni View Post
    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
  •  



Click Here to Expand Forum to Full Width