+ Reply to Thread
Results 1 to 6 of 6

VBA: Run function by function's name given as variable

  1. #1
    Registered User
    Join Date
    12-14-2011
    Location
    Chicago, IL, USA
    MS-Off Ver
    Office 2010
    Posts
    3

    VBA: Run function by function's name given as variable

    Hi there,
    I'm only 4 months experienced in the VB code, but I caught on pretty quick. A big hurdle that has been a tedious hassle has been accessing a particular function through a sorting function.

    For ex, I specify "Device A" on Sheet1 where I would have the options of "Device A", "Device B" or "Device C." I need it now to run the function "DeviceA" when I have the private functions "DeviceA" "DeviceB" and "DeviceC" available. So how can I use "Device A" as a variable value to run Private Function DeviceA?

    My current method is primitive where I wrote another private function with a case selection on that string "Device A." But now that I have over 50 different devices and keep adding new ones, I keep having to add more lines to that guiding private function and it is becoming difficult. With that, I have just been notified that I'm being reassigned in a month and need to train a less experienced draftsman this program in 3 weeks, so I need to laymen-ize this process.

    Any help will be appreciated!

  2. #2
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: VBA: Run function by function's name given as variable

    Try something like this:

    Please Login or Register  to view this content.
    Ted
    "Live Long and Prosper"

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA: Run function by function's name given as variable

    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context.
    Show a before and after situation and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    In addition let us know what is your overall aim. It may be that there are other simpler ways of dealing with your requirement that don't involve your current approach.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    12-14-2011
    Location
    Chicago, IL, USA
    MS-Off Ver
    Office 2010
    Posts
    3

    Re: VBA: Run function by function's name given as variable

    I'm sorry this is my first time posting on this forum, but how do I post a scrollable VB like @tkowal did above?

  5. #5
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: VBA: Run function by function's name given as variable

    type:
    ["code"]
    your vb code here
    ["/code"]

    Without the quotes in the [ ]
    Last edited by tkowal; 04-23-2012 at 04:43 PM.

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA: Run function by function's name given as variable

    Are all of the device functions the same or very similar? If so it might be more efficient to combine them into a single function with the device name as a parameter.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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