+ Reply to Thread
Results 1 to 7 of 7

Calling subroutines

  1. #1
    Registered User
    Join Date
    11-02-2008
    Location
    UK
    Posts
    4

    Calling subroutines

    I have a problem that from what I've read should be easy enough to do, but it is driving me mad.

    I have an Excel2007 workbook with VBA subroutine which is attached to one of the worksheets. I have placed it there because the worksheet has to be present for the code to work correctly.

    This works fine, but I would also like to be able to call it from another worksheet within the same workbook. From what I've read most people seem to say it is simply a matter of calling the subroutine :

    Call InsertJournal(2)

    Trying to run this code, however, gives me a compile error, sub or function not defined.

    Also, is it possible to check the existence of a worksheet with the name "Journals" beforehand to avoid errors?

    Thanks in advance

    Kevlaw

  2. #2
    Registered User
    Join Date
    11-02-2008
    Location
    UK
    Posts
    4
    Putting the code into a module rather than worksheet 'code' solves the problem, but how do I check for the existence of the worksheet before running?

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, Have you created your sub from the VBwindow, Toolbar, "Insert", "Module" , or not???
    It won't run from Private code module.
    Mick

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    try this

    Works even if it is a Private Sub
    Replace book, sheet & macro name to suit
    Please Login or Register  to view this content.
    Note I don't have Excel 2007 but it eorks in 2003 so shopuld work in 2007
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi, This should do:-
    Please Login or Register  to view this content.
    Regards Mick

  6. #6
    Registered User
    Join Date
    11-02-2008
    Location
    UK
    Posts
    4
    Thanks both,

    I've actually moved the code from the 'code' associated with the worksheet, and into a module attached to the same workbook.

    This now works fine.

    As the worksheet it affects is needed in all such workbooks there shouldn't be a problem, but is there a way to check the existence of a sheet with a particular name, as I expect colleagues of mine will eventually use this workbook, and I'd like a safeguard.

  7. #7
    Registered User
    Join Date
    11-02-2008
    Location
    UK
    Posts
    4
    Thanks Mick, in the time it took me to type the above you'd already answered it.

    Thanks very much

+ 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