+ Reply to Thread
Results 1 to 10 of 10

Object variable not set

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Object variable not set

    Hey all,

    the following piece of code keeps giving me Object variable not set error (below bolded).. and I am not sure why.. if anyone can fix it up / tell me whats wrong I would really appreciate it!

    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Object variable not set

    You can't combine those tests in a single statement -- if the range is Nothing, testing the row gives a runtime errror.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Object variable not set

    Hi thanks for the reply shg,

    So you're saying that if the range is nothing, it can't execute the second part of the test? that it will give a runtime error?
    Please Login or Register  to view this content.
    How can I test for both "non nothing" range as well as a specific value for the same cell?

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Object variable not set

    I'll make two observations (neither witch directly answer your question).

    (1) How are you using "year"? What is it? It is also a VBA function. If you are trying to use it as a function, you are using it wrong. And if you are trying to use it as a variable, you shouldn't.

    (2) As for the line:
    Please Login or Register  to view this content.
    There is no need for Val(). And is "Cells" supposed to reference the Active worksheet? Or "With unionrange"? If it is supposed to reference unionrange then you need a dot (period) before "Cells".

    If none of this helps, I apologize.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Object variable not set

    per shg's comment you need two tests
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Object variable not set

    I believe what Shg is suggesting is:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Object variable not set

    Thanks stephen ..

    definitely your advice helps - I'm learning every step of the way

    1.) You're right I should rename "year" as it is being used as a variable .. didn't think of the VBA function at the time of naming
    2.) I removed Val() and added a dot in front of cells as it is part of "With unionrange" thanks!

    however I still get an error with object variable or with block column not set error as shg pointed out

  8. #8
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Object variable not set

    Can you upload a workbook?

    P.S. You should have "Option Explicit" at the top of each of your modules. That would help you catch the "year" problem.

    Also, in the VBE (Visual Basic Editor), go to Tools -> Options and click the "Editor" tab and check the box next to "Require Variable Declarations" (if not checked). (This is not necessary, but good practice. Only needs to be done once.) Then any new modules will come with "Option Explicit" at the top of each modules. But you will still need to add it to all the old modules. Doing this helps catch all sorts of mistakes.
    Last edited by StevenM; 06-11-2012 at 11:45 AM.

  9. #9
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Object variable not set

    Actually this did the trick! Works perfectly now ...

    Thanks guys for the repeated help over multiple threads .. I think its great that there are people willing to go out of their way to help beginners like me

    Cheers and have a great day

    Quote Originally Posted by shg View Post
    You can't combine those tests in a single statement -- if the range is Nothing, testing the row gives a runtime errror.
    Quote Originally Posted by JosephP View Post
    per shg's comment you need two tests
    Please Login or Register  to view this content.
    Quote Originally Posted by StevenM View Post
    I believe what Shg is suggesting is:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Object variable not set

    and yeah stevenM, I did have option explicit on .. just this code wasn't the only code in the module so thats why I didn't copy option explicit .. and I just found out if I hover over the code in break mode - it will actually show me where my problem is (.. eventually)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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