Consulting

Results 1 to 10 of 10

Thread: What object is missin here (error 424)

  1. #1

    What object is missin here (error 424)

    I've read the "help" and as far my competence reaches, there's no errors in this code. VBA refuses to agree and usually, it's right. What's wrong here?

    [vba]
    Sub DoStuff()
    Dim sel As Range
    Set sel = Selection
    DoStuffToRange (sel) ' here VBA claims is an 424-error
    End Sub

    Sub DoStuffToRange(ByRef r As Range)
    For Each c In r
    MsgBox c.Value
    Next c
    End Sub
    [/vba]

    I also tried Selection.Cells (the same error) and Selection.Range (different error).

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    just lose the brackets (and declare all variables)
    [VBA]
    Sub DoStuff()
    Dim sel As Range
    Set sel = Selection
    DoStuffToRange sel ' here VBA claims is an 424-error
    End Sub

    Sub DoStuffToRange(ByRef r As Range)
    Dim c As Range
    For Each c In r
    MsgBox c.Value
    Next c
    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Is this a possible entry into the warning (error) database set up by Doug?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    By way of explanation ...

    Your variable sel is a Range so, without the parentheses, the Range is passed as a parameter.

    Parenthetical expressions are evaluated before anything else is done so, with the parentheses, VBA evaluates sel, which, in context, makes it look at the default property (.text) and so you end up with a String being passed instead of the Range.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Actually I forgot this was Excel ..

    So the default property of the selection is probably .Value and may be of varying types - double, variant array, etc., but not Range.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    Got it earmarked Ted- Thanks
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  7. #7
    Quote Originally Posted by TonyJollans
    Your variable sel is a Range so, without the parentheses, the Range is passed as a parameter.

    Parenthetical expressions are evaluated before anything else is done so, with the parentheses, VBA evaluates sel, which, in context, makes it look at the default property (.text) and so you end up with a String being passed instead of the Range.
    Whom shall one hate for this?

    The compiler should notify the programmer about such a thing much more descriptively. I guess it makes sense once one gets that explained but still... There's a packages of pure hate outside my office. Come and pick it up, whomever it might concern...

    Otherwise, i like working with VBA but some part are just too much.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    and for completion, if you use Call, then you need the brackets.
    [VBA]
    Call DoStuffToRange(sel)
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by chamster
    The compiler should notify the programmer about such a thing much more descriptively. I guess it makes sense once one gets that explained but still... There's a packages of pure hate outside my office. Come and pick it up, whomever it might concern...
    Why the hate. You got it wrong ... live with it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    And when you move to VB.Net or VB2005, you can unlearn that and use parentheses all the time...
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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