+ Reply to Thread
Results 1 to 10 of 10

Prevent VBA Functions from being available on worksheet

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Prevent VBA Functions from being available on worksheet

    Hi,
    I use a few Functions in my VBA code. All these functions, are declared as "Public", and reside in a single module. However, they are called from many different modules during code execution. (i.e. many functions called from many modules - hence the "Public" declaration).

    My issue is that in addition to being available to different VBA Modules, these Functions are also available on the worksheet as a UDF (so if a user presses "=" in the formula bar, the auto-complete shows these functions when the first characters match). Is there a way to remove the availability of the function on user worksheet? i.e. to allow a Function to be called from different modules in VBA, but prevent it from being available on the worksheet.

    My current work-around is to prefix all Public Function names with letter "j" - as no excel formula seems to begin with it - none of them show up as auto-complete options. Nevertheless, the Functions are still available to the user - which is what I would like to prevent.

    Thanks & regards.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Prevent VBA Functions from being available on worksheet

    Make them PRIVATE, that's the only reason to use private, to hide them from the user function list. Private functions are fully available to all code modules, make sure they are stored in code modules, not sheet modules or the ThisWorkbook module.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Prevent VBA Functions from being available on worksheet

    I declared a Function as Private and then called it from a procedure in another module and received the following error (along with the Function name highlighted):
    Sub or Function not Defined
    (Both, the calling procedure as well as the Private Function are on different modules - but neither of them is on a Class Module, or Userform, or Workbook/Worksheet object such as ThisWorkBook).

    Further, among the many reasons suggested by MS Excel Help regarding the error, one of them is:
    Sub, Function, or Property not defined (Error 35)
    The specified procedure isn't visible to the calling procedure.
    Procedures declared Private in one module can't be called from procedures outside the module.
    From the above, it would appear that if a Function is declared as "Private", it can only be called by other functions or procedures residing within the same module. Or did I miss something .

    I've double checked all spellings etc... As a matter of fact, if I simply change the Function from "Private" back to "Public", the Procedure works fine.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Prevent VBA Functions from being available on worksheet

    Apologies, I never came across that before. I guess I've always built my projects fully into one module.

    So, the next obvious fallback is to not use private function, but to use SUBs with parameters.

    Outside of the routines you declare a public variable. Then in your main macro you call the subroutine, feeding in the parameters needed, making sure the result of that macro is the public variable is filled in, thus available for use back in the main macro when it's done.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Prevent VBA Functions from being available on worksheet

    Yes, that would be a possible workaround. I was hoping there would be something at the declaration level itself, or some kind of toggle to allow/disallow VBA Functions as UDF.
    Nevertheless, many thanks for the help. I can work with this.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Prevent VBA Functions from being available on worksheet

    You can just add
    Please Login or Register  to view this content.
    to the top of the module containing your functions. This will hide them from the function wizard and the function dropdown, although if the user knows the name of the function he can still type it into a cell.
    Last edited by romperstomper; 07-10-2014 at 03:50 AM.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,193

    Re: Prevent VBA Functions from being available on worksheet

    If you make it a private module, won't that restrict its availability to the project in which it resides?

    If I have understood correctly, the OP wants the functions to be generally available to all VBA projects but not appear in the worksheet function list. I guess, for example, he may have a common function to determine the last row of data. This would not be useful on a worksheet.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,193

    Re: Prevent VBA Functions from being available on worksheet

    Sorry, RS. Just re-read the OP and I think I may have misunderstood the requirement. For functions, and subroutines, in a VBA project, I would normally declare all modules as Private with only, maybe, a top level calling routine as normal. Or, if the routines were called from a form or command button, everything would be in private modules.

    Regards, TMS

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Prevent VBA Functions from being available on worksheet

    romperstomper , TMS,
    Option Private Module doesn't seem much more helpful than my original choice of starting a function name with letter "j" - in that it prevents the Functions from showing up via auto-complete, but do not specifically restrict the user from calling them from a workbook as a UDF.

    I was hoping that there was some functionality built into Excel/VBA (maybe something like a Private Module can still be called via Module_Name.Function_Name from a procedure residing in another Module, but otherwise keeping it unavailable for the workbook)

    Nevertheless, many thanks for taking time out to respond to this one.
    Cheers!

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Prevent VBA Functions from being available on worksheet

    Option Private Module has the added benefit that the functions are not listed in the function wizard. So a user has to know what they are called to actually use them in a sheet. There isn't really any way to prevent that besides perhaps adding code to each one to ensure that application.caller is not a range.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Prevent copying of worksheet
    By DJvdW in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-14-2012, 09:22 AM
  2. [SOLVED] Excel 2007 : Prevent Worksheet Scrolling
    By HangMan in forum Excel General
    Replies: 5
    Last Post: 07-09-2012, 11:51 AM
  3. How to Prevent Worksheet from Deleting
    By taratata in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-01-2006, 03:45 AM
  4. Prevent Worksheet Deletions
    By Karen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-12-2005, 05:05 PM
  5. prevent saving a worksheet
    By looking4help in forum Excel General
    Replies: 2
    Last Post: 03-17-2005, 07:06 PM

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