locked
automatic button click event . RRS feed

  • Question

  • automatic button click event . Hello, I have a problem in an application I am developing in vb6 ,

    I need to generate the click event of a aoutomatica or dynamically button,

    the problem is I have 12 buttons that execute exactly the same code and let me know

    if there is some method to save code , I hope you can help me , greetings

    Wednesday, September 24, 2014 6:51 PM

Answers

  • Hi aslf010990,

    I am sorry, but this forum is not for Visual Basic 6 related questions, but for Visual Basic for Applications.
    Microsoft does not have a specific forum for Visual Basic 6, only for migration from Visual Basic to VB.Net.

    There are other communities where you can post your questions and might get help you needed.

    See for more info below link:

    Visual Basic 6.0 Resource Center

     

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Wednesday, September 24, 2014 8:05 PM
    Wednesday, September 24, 2014 7:36 PM
  • What are you trying to accomplish?

    You want to generate a click event for a Button within Excel?

    Do you know the name of the Button?

    You can simply execute the Click Event of a button, something like this:

    Private Sub CommandButton1_Click()
    MsgBox "Execute click event of CommandButton1!"
    End Sub
    
    Sub run()
    
    Call CommandButton1_Click
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Wednesday, September 24, 2014 10:54 PM
    Wednesday, September 24, 2014 10:42 PM
  • You mean something like this:

    Private Sub CommandButton1_Click()
    
     Dim o As OLEObject
     Set sh = ActiveSheet
    
     For Each o In sh.OLEObjects
    
        If o.Name = "CommandButton1" Then
         o.Object.BackColor = vbRed
         Else
         o.Object.BackColor = vbGreen
         End If
     Next o
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Thursday, September 25, 2014 2:04 PM
    Thursday, September 25, 2014 5:13 AM

All replies

  • Hi aslf010990,

    I am sorry, but this forum is not for Visual Basic 6 related questions, but for Visual Basic for Applications.
    Microsoft does not have a specific forum for Visual Basic 6, only for migration from Visual Basic to VB.Net.

    There are other communities where you can post your questions and might get help you needed.

    See for more info below link:

    Visual Basic 6.0 Resource Center

     

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Wednesday, September 24, 2014 8:05 PM
    Wednesday, September 24, 2014 7:36 PM
  • Ok Daniel, thanks.
    I really am developing a macro in excel with vba, when I referred to vb6 it is because I was wrong, instead of writing vba wrote vb6, sorry, back to the subject, once the point clear I hope you can help me with my doubt, Greetings
    Wednesday, September 24, 2014 8:09 PM
  • What are you trying to accomplish?

    You want to generate a click event for a Button within Excel?

    Do you know the name of the Button?

    You can simply execute the Click Event of a button, something like this:

    Private Sub CommandButton1_Click()
    MsgBox "Execute click event of CommandButton1!"
    End Sub
    
    Sub run()
    
    Call CommandButton1_Click
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Wednesday, September 24, 2014 10:54 PM
    Wednesday, September 24, 2014 10:42 PM
  • what happens when clicking on a button that changes its background color, if the button has not been pressed its background color is green but if pressed changes its color to red, as I mention I have 12 buttons and do not want to do what next to each button:

    Private Sub CommandButton1_Click ()
      Button1.BackColor = Color.Red
    End Sub
    Private Sub CommandButton2_Click ()
      Button2.BackColor = Color.Red
    End Sub
    Private Sub CommandButton3_Click ()
      Button3.BackColor = Color.Red
    End Sub

    what I want is to detect which button was clicked and change the background color, the only thing I want to achieve is to reduce slightly the number of lines of code, Greetings
    Wednesday, September 24, 2014 10:56 PM
  • You mean something like this:

    Private Sub CommandButton1_Click()
    
     Dim o As OLEObject
     Set sh = ActiveSheet
    
     For Each o In sh.OLEObjects
    
        If o.Name = "CommandButton1" Then
         o.Object.BackColor = vbRed
         Else
         o.Object.BackColor = vbGreen
         End If
     Next o
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Please vote an answer helpful if they helped. Please mark an answer(s) as an answer when your question is being answered.

    • Marked as answer by aslf010990 Thursday, September 25, 2014 2:04 PM
    Thursday, September 25, 2014 5:13 AM
  • thanks , just what I wanted , Greetings
    Thursday, September 25, 2014 2:07 PM