Results 1 to 5 of 5

Thread: [Excel] Opening other workbooks

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    [Excel] Opening other workbooks

    Hi,

    I'm fairly new to VBA, but have used other languages before, and I'm trying to open a second workbook and copy data across from it.

    To try and teach myself this, I tried to just open a second workbook and write to it but it's not working. I also tried to add some message prompts to see how far it's getting, but they are not appearing as well. (Though the second work book does open on the screen)

    My code is:
    Code:
    Sub Copy()
    
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    
    Set wbk = Workbooks.Open(FileToOpen)
    MsgBox "Test2"
    wbk.Sheets("Sheet1").Range("E16").Value = "Bob"
        
    MsgBox "Test"
    
    End Sub
    Thanks for any help,
    Paul

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: [Excel] Opening other workbooks

    Try this to get the file name:

    Code:
        filetoopen = Application.GetOpenFilename _
            ("Excel Files (*.xls), *.xls", _
            , "Please choose a file to import!")

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [Excel] Opening other workbooks

    Welcome to VBForums

    What's not working with your code?

    You are using, "wbk.Sheets("Sheet1")", that assumes that there is a sheet with this exact name.
    If not it will raise an error. You could use "wbk.Sheets(1)" , which reflect the first sheet without knowing its name (could be different in other languages!).
    At the end of the code you should close the workbook (wbk.close).
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Re: [Excel] Opening other workbooks

    Thanks for your help.

    Changing the open command to Workbooks.Open(filetoopen, True, True) seems to have sorted it.

    Thanks,
    Paul

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel] Opening other workbooks

    the second true is to open readonly
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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