Consulting

Results 1 to 12 of 12

Thread: Solved: Problem when trying to use close method of workbook object

  1. #1

    Solved: Problem when trying to use close method of workbook object

    Hi,

    I am trying to close a workbook using the close method of workbook object. Here is the code I am trying:

    [VBA] Sub close()
    Dim wb As Workbooks
    Set wb = Application.Workbooks
    'Trying to set savechanges to true
    wb("book1.xls").Close (True)
    End Sub[/VBA]

    I get an error saying " Run time error 9 : Subscript out of range"

    Please help me resolve this. I am new to excel VBA programming.Thanks!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub close()
    Dim wb As Workbooks
    Set wb = Application.Workbooks("book1.xls")
    'Trying to set savechanges to true
    wb.Close SaveChanges:=True
    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

  3. #3
    Thanks xld. However when I try this I get the following error message:
    Compile error: Wrong number of arguments or invalid property assignment

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Try this
    [vba]
    Sub CloseWB()
    Dim wb As Workbook
    Set wb = Application.Workbooks("book1.xls")
    'Trying to set savechanges to true
    wb.Close SaveChanges:=True

    End Sub

    [/vba]
    Note I would strongly recommend you don't use something like Close for the name of a Sub/Function, it's a VBA method.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by anandks42
    Thanks xld. However when I try this I get the following error message:
    Compile error: Wrong number of arguments or invalid property assignment
    Sorry, didn't spot the Dim statement.

    Change

    Dim wb As Workbooks

    to

    Dim wb As Workbook
    ____________________________________________
    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

  6. #6
    Thanks Norie and XLD but I am still getting the "subscript out of range" error. I am using excel 2003.

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Runtime Error 9 Subscript out of range basically means that the worksheet /workbook name cannot be found, check the name in the code for the name of your worksheet/workbook check spelling/spaces.
    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)

  8. #8
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Do you actually have a saved workbook named Book1.xls?

    By the way the original code you posted doesn't even compile.

    So I don't quite see how you would get any error message because it wouldn't run.

  9. #9
    Thank you Simon. I had not saved the workbook as book1.xls and hence the error.
    Norie, I ran the code without compiling it! The issue is solved.

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Eh, the code wouldn't run if it didn't compile.

    Unless you've changed some strange setting somewhere.

    If I paste the original code into a module the first line is highlighted in red, indicating a syntax error.

  11. #11
    I had not named the sub as close when i tried it. I had named it as temp. I renamed it when I typed it here as I thought the name would be more appropriate. That is why i did not get the syntax error!

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Please when posting code, post the actual code.

Posting Permissions

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