+ Reply to Thread
Results 1 to 8 of 8

range object as a function parameter problem

  1. #1
    Registered User
    Join Date
    08-31-2010
    Location
    Brighton,Earth
    MS-Off Ver
    Excel 2003
    Posts
    25

    range object as a function parameter problem

    Not sure why this is not working, as one can see I am simply trying to pass a range object but I get this error. What am I missing here?

    runtime error 1004
    method 'range' of object '_Worksheet' failed

    Please Login or Register  to view this content.
    Excel VBA 2003
    Brace yourselves for lots of stupid questions !!!

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: range object as a function parameter problem

    it looks like you're trying to get the highest number in cell myrange of all the worksheets. I'd recommend using this instead:

    Please Login or Register  to view this content.


    Basically the change is to pass a string that is the desired cell coordinate instead of trying to pass a range.

    Hope this helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    08-31-2010
    Location
    Brighton,Earth
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: range object as a function parameter problem

    Greetings tigeravatar,
    Sorry I should have made it clear this is just a useless piece of code I am using to practice and am not actually using it for anything. Can you explain why passing a range object and then referencing as I am trying to do but failing miserably won't work. I have tried passing a string with success but as I have said I am trying to understand the language.

    Cheers
    Last edited by romperstomper; 03-30-2011 at 05:02 PM. Reason: remove quote

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: range object as a function parameter problem

    By passing a Range object, you are passing a specific cell (or cells) on a specific sheet. If you want to refer to cells in the same location on another sheet, you need to refer to MyRange's address:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: range object as a function parameter problem

    guss,

    Aaah, ok. I see what you mean now. The problem is that when you define a range, you generally have to assign it to a worksheet.

    Originally stated: "The common workaround for this problem is to assign the range to ActiveSheet, and when you pass it through a function, the function cycles through worksheets and activates them. It would look like this using your example code:"

    Edit: That does not work. Thank you to romperstomper for correcting me. I apologize for the confusion.
    (incorrect code removed)
    Last edited by tigeravatar; 03-30-2011 at 05:32 PM. Reason: Updating post to reduce viewer confusion, remove incorrect answer

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: range object as a function parameter problem

    @tigeravatar:
    Your code does not do what I think you think it does. MyRange always refers to A1 on the sheet that was active at the start of the code, not A1 on whatever sheet is activated during your loop.

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: range object as a function parameter problem

    romperstomper,

    Oh, you're right. I just tested it (should have done that first, heh) and it does indeed specify itself to the sheet that was active when it was defined. Thanks for the correction, sorry for the confustion

    ~tigeravatar

  8. #8
    Registered User
    Join Date
    08-31-2010
    Location
    Brighton,Earth
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: range object as a function parameter problem

    Quote Originally Posted by romperstomper View Post
    @tigeravatar:
    Your code does not do what I think you think it does. MyRange always refers to A1 on the sheet that was active at the start of the code, not A1 on whatever sheet is activated during your loop.

    Amazing didn't know this and can see what you are saying, will have to play around with this to fully understand it.

    Many thanks romperstomper

    and thank you tigeravatar for trying to help me.

    Guss

+ 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