+ Reply to Thread
Results 1 to 7 of 7

Runtime error 9 - can't find the problem

  1. #1
    Registered User
    Join Date
    07-24-2013
    Location
    Bruges, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Runtime error 9 - can't find the problem

    Hi Everyone,

    I'm new to VBA and to this forum. I have written a piece of code and it keeps giving me a runtime error 9. Can somebody explain me why it is.

    The purpose of the macro is to create a new file based on the value of a cell and then copy some worksheets from the source file to the target file and then, using advanced filter, copy some rows to the target file.
    At first i tried to assign all worksheet and workbook names to variables but then i got runtime error 9's for almost every line.

    Can somebody please help, i'm looking now for 3 hours plus to solve the problem but i can't make it work. And if possible, to make it work so that the names of worksheets and workbooks are variables.

    The runtime error i get know is with
    Please Login or Register  to view this content.
    Many thx in advance!!

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error 9 - can't find the problem

    In that code you have this.
    Please Login or Register  to view this content.
    There's no workbook reference fo Sheets("ACTIVITEITEN").

    That normally isn't a problem but creating the new workbooks means there are now at least 2 workbooks open in Excel.

    VBA needs to know which workbook Sheets("ACTIVITEITEN") is in, so try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-24-2013
    Location
    Bruges, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 - can't find the problem

    Many thanks Norie!! Works perfect now!!! I thought it would be something as simple/stupid as this...

    And if, instead of working with the actual workbook and worksheet names, i would like to work with variables. How do have to do that? I tried it before but like i mentioned it gave runtime error 9's for almost every line... so i'm almost sure that i didn't declare the variables correct...

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error 9 - can't find the problem

    What do you want variables for?

    You are using variables in the top half of the code but in this part of the code you are using workbooks/worksheets names.
    Please Login or Register  to view this content.
    Are the workbooks in this code different from those at the start of the code?

  5. #5
    Registered User
    Join Date
    07-24-2013
    Location
    Bruges, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 - can't find the problem

    instead of using "P1515.xlsm", "bron.xlsm" and "BLANCO"... i would like to have them in variables because the P1515 is depending on the input of the user, and i will need this code again for other sheets...

    i know P1515 is stores in the variable flName, but when i try to use it in Workbooks("flname") it doesn't work, the same when i put the name of a sheet in a variable and try to use in Sheets("variable") it doesn't work... -> runtime error 9

    otherwise, i will try to rewrite it with variables and if it doesn't work i'll post my code again here

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Runtime error 9 - can't find the problem

    So are the workbooks you refer to by name in the second part of the code the same ones as in the first part?

    For example Workbooks("P1515") is NewWkbk, Workbooks("bron.xlsm") is ThisWorkbook.

    If that's the case perhaps this.
    Please Login or Register  to view this content.
    Last edited by Norie; 07-25-2013 at 09:28 AM.

  7. #7
    Registered User
    Join Date
    07-24-2013
    Location
    Bruges, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error 9 - can't find the problem

    Again many many thanks Norie!!! It works PERFECT!!! I'm going to compare what you did with my attempt and learn from it...

    Thx man!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] FIND function returns Runtime Error 91 in VBA
    By Ivkosky in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-25-2012, 05:08 PM
  2. Runtime Error 91 on Cells.Find
    By NickS89 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-31-2011, 01:26 PM
  3. Runtime error 3024 could not find file
    By vbqueen in forum Access Tables & Databases
    Replies: 2
    Last Post: 09-14-2010, 10:46 PM
  4. 1004 Runtime error problem
    By jp001 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-20-2009, 03:23 AM
  5. runtime error 9 problem with this code
    By cuewoz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-03-2006, 09:54 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1