+ Reply to Thread
Results 1 to 3 of 3

Issue with trying to pass name of command button as a variable

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Question Issue with trying to pass name of command button as a variable

    Hello All,

    I'm trying to pass name of the command button as a variable to ButtonName first before setting cmdButton.

    The name of the button is "Button_CalculationMode"

    Sub LoadCalcMode()
    
    Dim cmdButton As CommandButton
    Dim ButtonName As String
    
    ButtonName = "Button_CalculationMode"
    
    Set cmdButton = ActiveSheet.ButtonName
    
    cmdButton.Caption = "This is the new caption."
    
    End Sub
    Alternatively, if I were to set the button name directly, such as below, it works.

    Sub LoadCalcMode()
    
    Dim cmdButton As CommandButton
    
    Set cmdButton = ActiveSheet.Button_CalculationMode
    
    cmdButton.Caption = "This is the new caption."
    
    End Sub
    No matter what I've tried from researching online, I keep getting various run-time errors such as 438: object doesn't support this property or method., or type mismatch errors.

    I am fairly new to vba. Thanks in advance!
    Last edited by smk224; 07-02-2018 at 11:09 AM. Reason: Solved

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

    Re: Issue with trying to pass name of command button as a variable

    Try using this syntax
    Set cmdButton = ActiveSheet.OLEObjects(buttonName).Object
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: Issue with trying to pass name of command button as a variable

    Quote Originally Posted by mikerickson View Post
    Try using this syntax
    Set cmdButton = ActiveSheet.OLEObjects(buttonName).Object
    That worked! Awesome Thank you!

+ 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. [SOLVED] Userform: Textbox Live Clock and Command Button issue
    By VAer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-13-2017, 02:39 PM
  2. [SOLVED] Help with the code to pass a variable into the command syntax
    By rv02 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2016, 05:05 PM
  3. Getting Variable not defined when trying to use command button.
    By superchew in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 10:56 AM
  4. Pass Values From Array to a Range using command button
    By blackrock41 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2013, 02:40 AM
  5. Macro/Command button - cell reference issue?
    By mhblake in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2012, 02:41 PM
  6. Passing String Variable from Command Button Macro to a List Box in same User Form
    By wz4np1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2012, 11:31 AM
  7. VBA Userform Issue - Command Button / msg Box
    By teeks in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-07-2010, 11:37 AM

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