Consulting

Results 1 to 8 of 8

Thread: Sub reference as Cell

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location

    Sub reference as Cell

    Hello,

    I have a sub such as:
    [VBA]

    Sub is_stuff_happening(rng As range)
    For Each C In rng
    Call set_word(C)
    Next C
    End Sub

    Sub set_word(C As ***)
    [/VBA]

    And I don't know what should be put in the place of the ***. Range doesnt work and there doesnt seem to be a cell reference. Does anyone know?
    Thanks in advance!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It should be Range.
    ____________________________________________
    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

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location
    Quote Originally Posted by xld
    It should be Range.
    When I do that though, it highlights the C in set_word and gives me the error:
    ByRef argument type mismatch

    What to do?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use Option Explicit

    [VBA]
    Sub is_stuff_happening(rng As range)
    Dim C as range
    For Each C In rng
    Call set_word(C)
    Next C
    End Sub

    Sub set_word(C As Range)
    [/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'

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Might have to add .Cells to the For Each loop


    [vba]

    Sub is_stuff_happening(rng As range)
    Dim C As range
    For Each C In rng.Cells
    Call set_word(C)
    Next C
    End Sub

    Sub set_word(C As Range)


    [/vba]

    Paul

  6. #6
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location
    Thanks for all the repsonses!

    So i have this
    [vba]

    Sub is_stuff_happening(rng As range)
    Dim C As range
    For Each C In rng.Cells
    If IsNumeric(C.Value) And IsNumeric(C.Offset(0, -1).Value) Then
    Call set_word(C)
    End If
    Next C
    End Sub

    Sub set_word(C As Range)


    [/vba]
    This now gives me another error, hilighting the If statement:

    Run-time error '1004':
    Application-defined or object-defined error


  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You will get an error if you try and access any cells in column A because of the Offset(0, -1)
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location
    Nice, i had the wrong cell input
    Thank you so much everyone!!!

    Quote Originally Posted by xld
    You will get an error if you try and access any cells in column A because of the Offset(0, -1)

Posting Permissions

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