Consulting

Results 1 to 5 of 5

Thread: Passing a cell to a sub routine

  1. #1

    Talking Passing a cell to a sub routine

    Hey, can anybody please tell me why you can not pass a cell to a sub

    eg

    Private Sub Worksheet_Change(ByVal Target As Range)
    For Each Cell In Target.Cells
    CellChanged (Cell)
    Next
    End Sub

    Private Sub CellChanged (Cell)
    if Cell.Column = 2 and Cell.Range = 2 and Cell.Value <> "" then beep
    End Sub

    Although I can see things like Cell.Column in Worksheet_Change, it appears that it only passes the value to CellChanged, not everything else.

    Is there anyway around this

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,366
    Location
    Hi there,

    Ditch the paranthesis if not using Call.

    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range

    For Each Cell In Target
    CellChanged Cell
    Next
    End Sub

    Private Sub CellChanged(Cell)
    If Cell.Column = 2 And Cell.Row = 2 And Cell.Value <> "" Then Beep
    End Sub[/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    The problem is that enclosing the object in parentheses causes is to be evaluated before being passed, that is why you should exclude them, or preferably in my view, use the Call construct.
    ____________________________________________
    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

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,366
    Location
    Quote Originally Posted by GTO
    Ditch the paranthesis if not using Call.
    Quote Originally Posted by xld
    The problem is that enclosing the object in parentheses causes is to be evaluated before being passed, that is why you should exclude them, or preferably in my view, use the Call construct.
    Hi Bob ,

    Thank you on both counts. While decent at spelling, there are a few words out there, that for whatever reason, just botch me, most every time. Aggravating to say the least.

    As to the explanation - I was not meaning to be/sound 'short' in the least, but was just busy and in actuality, passed the parenthesis by a number of times...

    Anyways - I am pretty sure you qualify for a 'mind-reading card' now, as, while I had given up on figuring exactly the why, when (before catching/seeing the parenthesis) stepping through, I was utterly jammed as to why the val kept arriving...

    Hope that makes sense, and again, while (or whilst) short, awfully succinct

    Mark

  5. #5
    Thankyou very much guys, I would check this out today

Posting Permissions

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