+ Reply to Thread
Results 1 to 6 of 6

control the command button

  1. #1
    Ming
    Guest

    control the command button

    Hi! All, Does anybody know how to use some VBA code in macro to control
    the command button on the worksheet? That means the code is used the
    same way as people click the command button. Thanks a lot!


  2. #2
    Norman Jones
    Guest

    Re: control the command button

    Hi Ming,

    Two suggestions:

    (1) Declare the CommandButton's click event Public and call it from a sub in
    a normal module, e.g.:

    In the Sheet module
    ---------------------
    Public Sub CommandButton1_Click()
    MsgBox "Hello Ming"
    End Sub

    In a normal module
    --------------------
    Sub Tester()
    ThisWorkbook.Sheets("Sheet1").CommandButton1_Click

    End Sub


    (2) Alternatively,

    In the Sheet Module
    ---------------------
    Sub CommandButton1_Click()
    call Tester2
    End Sub

    In a normal module
    --------------------
    Sub Tester2()
    MsgBox "Hello Ming!"
    End Sub

    In both cases, clicking the button or invoking the ssub in the standard
    macro are equvalent.


    ---
    Regards,
    Norman



    "Ming" <[email protected]> wrote in message
    news:[email protected]...
    > Hi! All, Does anybody know how to use some VBA code in macro to control
    > the command button on the worksheet? That means the code is used the
    > same way as people click the command button. Thanks a lot!
    >




  3. #3
    Norman Jones
    Guest

    Re: control the command button

    Hi Ming,

    > In both cases, clicking the button or invoking the ssub in the standard
    > macro are equvalent.


    Should have read:

    In both cases, clicking the button, or invoking the Sub in the standard
    module are equivalent.

    ---
    Regards,
    Norman



    "Norman Jones" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Ming,
    >
    > Two suggestions:
    >
    > (1) Declare the CommandButton's click event Public and call it from a sub
    > in a normal module, e.g.:
    >
    > In the Sheet module
    > ---------------------
    > Public Sub CommandButton1_Click()
    > MsgBox "Hello Ming"
    > End Sub
    >
    > In a normal module
    > --------------------
    > Sub Tester()
    > ThisWorkbook.Sheets("Sheet1").CommandButton1_Click
    >
    > End Sub
    >
    >
    > (2) Alternatively,
    >
    > In the Sheet Module
    > ---------------------
    > Sub CommandButton1_Click()
    > call Tester2
    > End Sub
    >
    > In a normal module
    > --------------------
    > Sub Tester2()
    > MsgBox "Hello Ming!"
    > End Sub
    >
    > In both cases, clicking the button or invoking the ssub in the standard
    > macro are equvalent.
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Ming" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi! All, Does anybody know how to use some VBA code in macro to control
    >> the command button on the worksheet? That means the code is used the
    >> same way as people click the command button. Thanks a lot!
    >>

    >
    >




  4. #4
    Ming
    Guest

    Re: control the command button

    Thanks a lot! Norman. Your suggestion is very helpful!


  5. #5
    Ming
    Guest

    Re: control the command button

    Hi! Norman, Just a short question: If I've got a CommandButton's click
    event which is Private instead of Publich, can I still call it from a
    sub in a normal module? Thanks again!


  6. #6
    Norman Jones
    Guest

    Re: control the command button

    Hi Ming,

    No a private procedure in a worksheet module cannot be called from a
    standard module.

    If you want to keep the button's click event private, use the second
    suggestion - which would be my first choice!

    ---
    Regards,
    Norman



    "Ming" <[email protected]> wrote in message
    news:[email protected]...
    > Hi! Norman, Just a short question: If I've got a CommandButton's click
    > event which is Private instead of Publich, can I still call it from a
    > sub in a normal module? Thanks again!
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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