+ Reply to Thread
Results 1 to 7 of 7

Button Detection

Hybrid View

Warderbrad Button Detection 08-05-2007, 01:50 PM
Norie What do you mean by a form? ... 08-05-2007, 02:10 PM
Warderbrad a "Form Button Control" from... 08-05-2007, 02:16 PM
Norie Well you should be able to... 08-05-2007, 02:21 PM
Warderbrad I had not heard of that... 08-05-2007, 02:51 PM
  1. #1
    Registered User
    Join Date
    10-17-2005
    Posts
    40

    Button Detection

    I have a form where I have 5 buttons that all will do the same thing just to different sections of the form, I would like to simplify my code by only having one routine for all five. My only sticking point is that I am not sure how to detect which button of the five was used. Basically each button will clear a small group of cells next to that button.

    Is there a way to detect the name of the form control that initiated the code to be ran?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    What do you mean by a form?

    Userform?

    Worksheet with buttons?

  3. #3
    Registered User
    Join Date
    10-17-2005
    Posts
    40
    a "Form Button Control" from the Forms Toolbar it is placed on a normal worksheet.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Well you should be able to use Application.Caller to get the name of the button that's been pressed and act accordingly.

  5. #5
    Registered User
    Join Date
    10-17-2005
    Posts
    40
    I had not heard of that before, thanks I will research how to use that.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Warderbrad,

    The Application.Caller will return a string which is the name of the Forms Button that was clicked. Using a Select Case statement is the easiest and most efficient method of selecting the code to be run. Replace the button names if yours are different.
    Sub ButtonMacro()
      Dim Btn As String
    
        Btn = Application.Caller
        
          Select Case Btn
            Case Is "Button 1"
              'Place Button 1's code here
            Case Is "Button 2"
              'Place Button 2's code here
            Case Is "Button 3"
              'Place Button 3's code here        
            Case Is "Button 4"
              'Place Button 4's code here
            Case Is "Button 5"
              'Place Button 5's code here
          End Select
    
    End Sub
    To run the same code if the button matches any 1 of the 5, use this example...
    Sub ButtonMacro()
    
      Dim Btn As String
    
        Select Case Btn
          Case Is "Button 1", "Button 2", "Button 3", "Button 4", "Button 5"
            'Place code here
        End Select
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 08-05-2007 at 04:24 PM.

+ 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