Consulting

Results 1 to 5 of 5

Thread: Can you call a Sub from a Function?

  1. #1

    Can you call a Sub from a Function?

    Title says it all. Can you?

    A function cannot modify a worksheet but a subroutine can. So can you call a subroutine from a function to modify a range of cells?

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by thepr1nter
    Title says it all. Can you?

    A function cannot modify a worksheet but a subroutine can. So can you call a subroutine from a function to modify a range of cells?
    Assuming you are refering to VBA functions and subs: not sure where you told that a function cannot modify a worksheet. They can and I have some that do. It is probably not a good practice to do that, but ...

    Functions can call subs or anything else. I have numerous functions that call subs which call other functions, etc. There may be a practical limit to "things calling things", but I have never encountered it.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You can Call a Sub, but you still cannot modify the worksheet if the Function is called from a cell. If the Function is called from a Sub then you have more options.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by thepr1nter
    Title says it all. Can you?

    A function cannot modify a worksheet but a subroutine can. So can you call a subroutine from a function to modify a range of cells?
    Of course you can, but if you try and modify the worksheet in a sub called from a function invoked within the worksheet it will fail.

    You don't get around it that easily <vbg>.

    .
    ____________________________________________
    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

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi thepr1nter, thanks for stopping back

    While you can call a sub from a function, you still can't manipulate a worksheet from a worksheet function calling a sub even if that sub has the actual code.

    Sorry
    Matt

Posting Permissions

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