+ Reply to Thread
Results 1 to 11 of 11

Getting an error when trying to call a function from a sub, with string value of cell

  1. #1
    Registered User
    Join Date
    07-30-2012
    Location
    Falls Church, VA
    MS-Off Ver
    2007
    Posts
    2

    Question Getting an error when trying to call a function from a sub, with string value of cell

    I have the following code:

    Please Login or Register  to view this content.
    When I run this, i get "Run-time error '424': Object required" on the line where the function is called. I've tried .Text, .Value, and just the cell itself, and they all get an error.

    Need help!

    Thanks!

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    There are a few things I am not sure about. For example the statement "a=Worksheets...CurrentRegion" would declare the variable "a" as a "Range" type.However that would require the Set instruction as well, and would then invalidate the usage of Ubound which can only be used for arrays. Assuming that you want to use the ranges, I have made a few changes.

    Please Login or Register  to view this content.

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

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    Try removing .Text after a(i,28).
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    07-30-2012
    Location
    Falls Church, VA
    MS-Off Ver
    2007
    Posts
    2

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    Thank you, Olaf. I am just learning this stuff, and got that code either from an example somewhere, or cobbled it together from several examples (which may explain why things aren't consistent).

    Anyway, you got me on the right path. I got it working, but still had to make a few changes. I had to change the function to return a Double instead of Integer, and had to say GetPersonId = pl(i, 1).Value in the for loop, since I kept getting overflow errors. But after that, it works!

    Thanks again!

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    Quote Originally Posted by RebelScum View Post
    Thank you, Olaf. I am just learning this stuff, and got that code either from an example somewhere, or cobbled it together from several examples (which may explain why things aren't consistent).

    Anyway, you got me on the right path. I got it working, but still had to make a few changes. I had to change the function to return a Double instead of Integer, and had to say GetPersonId = pl(i, 1).Value in the for loop, since I kept getting overflow errors. But after that, it works!

    Thanks again!
    Great that you find the remaining problems. Can you please update the title of your post to indicate that it has been solved? Thank you.

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

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    norie was correct-your sub was wrong not the function
    Please Login or Register  to view this content.
    you didn't need Set because you were converting a range into an array of values (and values do not have a Text property which is what caused the error)
    Josie

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

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    I see. Without explicit declarations and comments it is sometimes hard to understand the purpose or intention of the code.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    Explicit declarations wouldn't help much here I'm afraid. Since a would just need to be declared as Variant

    you can assign the values of a range to a 2 dimensional array (rather than a range) using the .Value property of the range object when the variable is a variant - it saves looping through the range to populate the array.

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    I understand. Had I seen that "a" was declared as a variant, I would have understood the array usage (eventually at least). Anyway, water under the bridge.

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

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    the code would have been better written to explicitly use the .value property
    Please Login or Register  to view this content.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Getting an error when trying to call a function from a sub, with string value of cell

    Indeed, I tend not to rely on default properties

+ 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