+ Reply to Thread
Results 1 to 18 of 18

Assign function to button

  1. #1
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Assign function to button

    Hi. I've spent a couple days already trying to figure out how to assign a function in a module to a button on a worksheet.
    I don't know VBA but i looked around trying to understand how to call the function from a subroutine so I could assign it to a form control button.
    Then I tried to do the same with and ActiveX button, same result. The debugger game me different errors as i tried with different combinations.
    The function (currency exchanger) works just fine. It starts like this:

    Please Login or Register  to view this content.
    And tried calling it mostly using this:

    Please Login or Register  to view this content.
    I also tried with Public and Private Subs and putting the function in the worksheet but I bet it requires more than that. Maybe declaring some variable?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Assign function to button

    You can't assign a function to a button.

    What is it you actually want the button to do?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    Hi Norie. I just want to execute the function to get exchange rates updated as needed.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,155

    Re: Assign function to button

    Just create a button and assign the macro to it. Or am I missing something?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  5. #5
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    The function can't be assigned as a macro. Not knowing VBA I can't tell you why but the function does not appear as an option. It is not considered a macro unless it is inside a subroutine (I think).

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Assign function to button

    You don't call a function like that - you need something like:
    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Assign function to button

    Perhaps CurrEx shouldn't be a function.

    How does it normally work?

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Assign function to button

    Buttons are for doing something.
    Functions return a value, but they don't do anything. (Which is why Excel allows macros to be assigned to a button, but not functions.)

    A button that does "Calculate the value" is kind of useless.
    A button that does "Calculate the value. Put that value in cell C1" is useful.

    When the user presses the button what changes do you want on the worksheet?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    Thanks xladept. The function allows you to type what currency ISO code to use, for example USD and GBP.
    I'm guessing the code you gave me implies I would need to set the two currencies in the function instead of being able to choose one on the sheet. Am I understanding it right?

  10. #10
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    Mikerickson, what I was trying to accomplish was not exactly calculating the value, but more like refresh the values selected in the sheet. USD to GBP for example.

  11. #11
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    Norie, the reason I'm using a function is because I found the code and it does what I needed it to do.
    I don't know VBA so I'm working with what I have. But in this case I would prefer to have something that could be assigned to a button.

    I have it set up so it would either get the exchange rate between two currencies, entering the ISO code. Or converting a certain amount from one currency to the other.
    Right now the only way to do it is to enter the ISO code again, meaning changing the value of the cell. But if I just want to refresh the current values on the sheet it would be easier to just press a Refresh button.
    Last edited by DMA; 07-09-2017 at 12:52 PM.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Assign function to button

    If you are already using the formula in cells on the worksheet they should automatically recalculate/refresh when a change is made on the sheet.

    If that's not happening there might be a problem with your function.

  13. #13
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    It does that. I just edited my previous post explaining how it works.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Assign function to button

    Yeah - you must specify both arguments - you're welcome.

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Assign function to button

    Hi,

    You can in fact assign a function to a button but there is usually little point to it which, I assume, is why it is not made obvious. Here I don't see how it could help.

    I suspect you might simply make the function volatile- add
    Please Login or Register  to view this content.
    after the function declaration line. You might also add a button that simply forces recalculation of the application.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  16. #16
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    xlnitwit, The volatile function is an interesting solution, except in this case, because of the web query, it creates a long delay on any change to the sheet.
    I guess the best option in this case will be the button to force recalculation.
    Thank you!

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Assign function to button

    You may be able to use the Range.Calculate method rather than calculating the entire workbook or, if you have multiple such function calls, simply link them all to one controlling cell and edit that value instead. Thus rather than a simple
    =CurrEx(A2,B2)
    type call, you can use
    =IF($A$1=1,CurrEx(A2,B2),"")
    in each call and then you need only re-enter the 1 in A1 to update all the UDF calls.

  18. #18
    Forum Contributor
    Join Date
    07-08-2008
    Location
    New York City
    MS-Off Ver
    2013-2016
    Posts
    159

    Re: Assign function to button

    That's a clever solution. I guess I'll use that one.
    Thanks for the help!

+ 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. Assign a Private Sub to a button
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-20-2014, 06:48 AM
  2. Custom Function to Create and Assign Button
    By dkang2007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2014, 02:44 PM
  3. Assign Macro to Button
    By DanBlum in forum Excel General
    Replies: 1
    Last Post: 02-20-2012, 10:42 AM
  4. Assign Macro to a Button
    By tt388 in forum Excel General
    Replies: 1
    Last Post: 10-26-2008, 09:40 AM
  5. Cannot assign macro to button....HELP!!!
    By fry in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-09-2007, 05:43 AM
  6. Cannot assign a hyperlink to a button
    By andrefrancis1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-02-2006, 04:48 AM
  7. [SOLVED] How can I assign a symbol to a button (like the $ button)
    By NickW in forum Excel General
    Replies: 2
    Last Post: 09-06-2005, 10:05 AM

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