Consulting

Results 1 to 14 of 14

Thread: Opening Workbook through command button

  1. #1
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location

    Opening Workbook through command button

    Hi All,

    If I want to open say: C:\Documents\WorkbookA.xls via a command button what is the code you use (If I have the command button already set up)?

    Thanks all.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]Private Sub CommandButton1_Click()
    Workbooks.Open ("C:\Documents\WorkbookA.xls")
    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location

    Smile

    Thanks Simon, much appreciated .

  4. #4
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    Just revisiting the above thread. I was wondering what is the syntax to use with Simon's code above to prevent updating links?

  5. #5
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location

    MACRO to open workbooks

    Hi aLL,

    I have been trying to code the folowing macro to open a few workbooks without updating external links.

    Although simple enough I keep running into a 1004 error.

    As an example,

    A1 = C:\Documents and Settings\LongFilenameA.xls

    A2 = C:\Documents and Settings\LongFilenameB.xls

    Now I use the following code:

    [vba]

    Sub openwkbks()

    Workbooks.Open Filename:=Range("A1").Value, UpdateLinks:=0
    Workbooks.Open Filename:=Range("A2").Value, UpdateLinks:=0

    End Sub

    [/vba]

    It opens A1 fine, but does not open A2 workbook and runs into the 1004 runtime error (see attached picture).

    Could Anyone please help me correct the above macro so as to ask it it to properly open A1 and A2 filepaths correctly?

    [Note: If I use the following code:


    [vba]

    Sub openwkbks()

    Workbooks.Open Filename:="C:\Documents and Settings\LongFilenameA.xls", UpdateLinks:=0
    Workbooks.Open Filename:="C:\Documents and Settings\LongFilenameB.xls", UpdateLinks:=0

    End Sub

    [/vba]

    This works, but doesn't serve my purpose as I ideally want to open A1:A10 every months and them in the VBA code]

    Regards

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That ius because the newly opened workbook becomes active, and the code will then look there for the next filename

    Use

    [vba]

    Sub openwkbks()
    Dim This As Worksheet

    Set This = Activesheet
    Workbooks.Open Filename:=This.Range("A1").Value, UpdateLinks:=0
    Workbooks.Open Filename:=This.Range("A2").Value, UpdateLinks:=0

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    The reason the first line works and subsequent ones fail is that your range reference is not fully qualified. It is using the active sheet as a reference.
    One the first workbook is opened the A2 is that of the recently opened workbook not the one with the relevant data.
    So set a reference to the sheet and use it.

    [vba]
    Sub openwkbks()

    Dim shtUse as worksheet

    set shtUse = Activeworksheet

    Workbooks.Open Filename:=shtUse.Range("A1").Value, UpdateLinks:=0
    Workbooks.Open Filename:=shtUse.Range("A2").Value, UpdateLinks:=0

    End Sub
    [/vba]
    Cheers
    Andy

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]
    Private Sub CommandButton1_Click()
    Workbooks.Open ("C:\Documents\WorkbookA.xls"),UpdateLinks:=False
    End Sub
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xluser2007
    Just revisiting the above thread. I was wondering what is the syntax to use with Simon's code above to prevent updating links?
    The same as the code that you used in your other thread!
    Last edited by Bob Phillips; 01-05-2008 at 01:17 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Threads merged
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Hmmmmm, merged threads......confuses the simpler folk......like me!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  12. #12
    helpfull for me ,as newbie

  13. #13
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    Thanks Simon, xld, Andy and lucas,

    Helpful as always!

    Also thanks for combining the threads Simon and posting the relevant code for my other query.

    regards

  14. #14
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I'll accept the thanks but even more confused now....guess my single braincell has little room for lateral accomplishments!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •