Run Command Button Click From Another Command Button

  • Let me say I have a userform with a button on it named But1. Is it possible to store But1 into a variable then activate the But1_Click action using a variable?

    Code
    Dim strButname as string
    strButname ="But1"


    Now, how would you activate the But1_Click method using strButname variable. With listboxes you can use the Control(strListboxName) methodology?


    This doesn't work, but gives you an idea of what I am trying to do.


    Controls(strButname)_click

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Activate The Button_click Method Via Variable/string


    No luck. Dumped and stated not found

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Activate The Button_click Method Via Variable/string


    I don't think you can assign a Button directly to a string variable. But why would you?


    Anyway a work around would be to have a UserForm with say three buttons and a list box with the following code behind the user form. The list box would be populated with the names identifying which button click code to run when selected.


    Note that the button click code must not be “private” if the code is called from outside the userform module. That is if the list box was on another userform.


    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Activate The Button_click Method Via Variable/string


    Concept would be for creating programmable buttons that user could specify series of buttons to be clicked. In essence, enable user to create a "Macro" on a form which would autoclick a series of buttons that they specify.

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Activate The Button_click Method Via Variable/string


    Lets see if I understand what you are saying.


    The user is presented in some fashion a selection of pseudo buttons. Each pseudo button represents a real button which has a button_click macro attached to it. The user can pick and chose which buttons to be added to a UserForm. After the first pseudo button is chosen a UserForm is created and the associate real button of the chosen first pseudo button is applied to the created UserForm . Then other pseudo buttons can be chosen and the associated real buttons will be added to the created UserForm.


    Is this what you are asking how to do?

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Activate The Button_click Method Via Variable/string


    Does this help?

    Code
    Private Sub CommandButton1_Click()
        CommandButton2_Click
    End Sub
    
    
    Private Sub CommandButton2_Click()
      MsgBox "I'm CommandButton2 Code"
    End Sub
  • Re: Run Command Button Click From Another Command Button


    humm, getting sidetracked. The goal here is creating a button, which the user can choose will fire a series of buttons when pushed. Basically it is like the Record macro button. Let say one user wanted buttons A, B, C fired when they pushed the super button. No another user may want C, B, A, D fired when they push the super button. I was trying to avoid endless barage of if statements. Basically the button names could be stored in configuration file as text strings and would load @ runtime for any users form. Thus a single function to activate the click command based on the text strings.

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Run Command Button Click From Another Command Button


    Something like this maybe.


    Userform with 5 buttons. cmdA to cmdE
    1 textbox
    1 commandbutton


    [vba]Private Sub cmdA_Click()
    MsgBox "cmdA"
    End Sub
    Private Sub cmdB_Click()
    MsgBox "cmdB"
    End Sub
    Private Sub cmdC_Click()
    MsgBox "cmdC"
    End Sub
    Private Sub cmdD_Click()
    MsgBox "cmdD"
    End Sub
    Private Sub cmdE_Click()
    MsgBox "cmdE"
    End Sub
    Private Sub CommandButton6_Click()


    Dim vntBut As Variant

    On Error Resume Next

    For Each vntBut In Split(UCase(TextBox1.Text), ",")
    Me.Controls("cmd" & vntBut).Value = True
    Next

    End Sub
    [/vba]


    In the textbox enter comma delimited list of buttons to PUSH.
    For example,


    e,a,c

  • Re: Run Command Button Click From Another Command Button


    ExcelJunkie, your post and thread title should always reflect what you are attempting to do, as apposed to what you THINK you need to achieve it. Same logic applies when searching.

  • VBA for Multiple Command Button


    Hi All,


    I am trying to write VBA for Command button. Herewith I have updated my queries in details. Please do the needful.


    1) I have multiple button in one of the worksheet like attached below. If I click button based on Column 1 value, it need to search data in second worksheet. [TABLE="class: grid, width: 500"]

    [tr]


    [td]

    A

    [/td]


    [td]

    1

    [/td]


    [td]

    Button 1

    [/td]


    [/tr]


    [tr]


    [td]

    B

    [/td]


    [td]

    1

    [/td]


    [td]

    Button 2

    [/td]


    [/tr]


    [tr]


    [td]

    C

    [/td]


    [td]

    1

    [/td]


    [td]

    Button 3

    [/td]


    [/tr]


    [/TABLE]


    2) Is there any way to create subroutine program while running an another macros.


    3) Is there any way to assign VBA code to button as action instead of macros. For example


    ActiveSheet.Shapes("Button 1").Select
    Selection.OnAction = "=sum(B1:B2)"
    Range("L20").Select

  • Re: Run Command Button Click From Another Command Button


    Please start your own thread. Give it a title that accurately and concisely summarises your issue and explain your problem fully.


    Also, 1 issue per thread please and that issue is as described in the thread title.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!