+ Reply to Thread
Results 1 to 2 of 2

Obect Required error when variable already set earlier in sub?

  1. #1
    Forum Contributor
    Join Date
    05-12-2014
    MS-Off Ver
    Excel 2013
    Posts
    131

    Obect Required error when variable already set earlier in sub?

    Hey folks! I have what I hope is an easy question to answer.

    So I have code where I'm setting the following variables:
    Please Login or Register  to view this content.
    For the first few lines, whenever I call on w1.Copy, it works great.

    However, if I have code later in the sub that looks like this:
    Please Login or Register  to view this content.
    I get an Object Required error in the debugger. Do I have to set w1-w5 every time even though the range never changes? I'm thinking it has something to do with the fact that I'm re-setting z to refer to a new worksheet, but I figured since I'm re-setting z I wouldn't need to reset the w's as well since those just call on the most recent z variable.

    But I'm also learning VBA as I go, so I know I'm using flawed logic here (if for no other reason than it isn't working :P). Is anyone able to let me know where I'm going wrong here, whether it be syntax or thinking?

    Thanks!
    Last edited by mrbusto71; 08-29-2016 at 07:18 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Obect Required error when variable already set earlier in sub?

    Quote Originally Posted by mrbusto71 View Post
    Do I have to set w1-w5 every time even though the range never changes?
    Yes. When you define a Range variable like you do with W1-W5, the range includes it's actual parent sheet and actual parent workbook. The range variable doesn't reference the variables (x and z) that were used when it was defined.

    Alternatively, you could use this syntax to define the range variables...
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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. Replies: 7
    Last Post: 05-19-2016, 05:12 AM
  2. [SOLVED] Setting a range variable to its offset triggers an "Object required" error
    By Durarara in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2016, 01:50 PM
  3. [SOLVED] New Error/Old Code - Run Time Error 91; Object variable or With block variable not set
    By humboldtguy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-15-2014, 12:39 AM
  4. Set Variable = File Path generates "Object Required" Error
    By Brian48072 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2013, 11:42 AM
  5. Unable to delete sheet due to macros copied earlier - error 400
    By talhawahab in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2012, 11:52 PM
  6. Variable Required Error
    By Baybars in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-26-2010, 07:37 PM
  7. Error. Variable Required
    By shapper in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-19-2008, 03:36 PM

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