Consulting

Results 1 to 10 of 10

Thread: Identify Button that has been clicked

  1. #1
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location

    Identify Button that has been clicked

    Hey there!
    I have an excel database of my music titles and added a "play"-function to directly play the title in excel.
    I want to add a play button for every title (one title per row), so it's going to be 1000+ buttons (this is the only reasonable method I could think of).
    If I'd do it with Control Buttons, I'd have to add 1000+ times the code for each button to play music, so I use Forms Buttons and want to assign them all to the same macro that will start playing.

    I want to program it like this:
    Public Sub play_Change()
        Dim buf As String
        buf = GetButtonNameThatActivatedThisMacro()
        'Buttons are named Button1, Button2, etc. so then I retrieve the Button number out
    'of the Button name and play the title in the row of this number
    End Sub
    Is this possible?
    TIA,
    Hawk
    Last edited by Aussiebear; 04-08-2023 at 12:49 PM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    One button per title? Why?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    I'd set it up do that if I double clicked the row with a music title, it'd play

    Lots easier that 1000+ buttons

    Paul

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I think what you're looking for is Application.Caller
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Hawkin
    ...so I use Forms Buttons and want to assign them all to the same macro that will start playing...
    Hi Hawkin,

    I'm not much with Shapes, so there may be better ways, but to go along with Malcom's suggestion, you could use the Shape's names.

    Option Explicit
        
    Sub CalledFromFormsButton()
    Select Case ActiveSheet.Shapes(Application.Caller).Name
        Case "Button 1":    MsgBox "Called from: " & ActiveSheet.Shapes(Application.Caller).Name
        Case "Button 2":    MsgBox "Called from: " & ActiveSheet.Shapes(Application.Caller).Name
        End Select
    End Sub
    Hope that helps,

    Mark
    Last edited by Aussiebear; 04-08-2023 at 12:50 PM. Reason: Adjusted the code tags

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    'Buttons are named Button1, Button2, etc. so then I retrieve the Button number out
    'of the Button name and play the title in the row of this number

    That's still 1000+ buttons on the worksheet !!!!!

    Each one has to be inserted, configured, named ...

    Since you want to play from Excel, I'd re-consider the 1000+ Buttons and use a more 'Excel-like' solution.

    Possibly, the WS BeforeDoubleClick even to take the cell and Play the title


    Paul

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'd go along with Paul.
    You could try formatting cells to resemble buttons if you really want this sort of interface, and use the Selection Change event to play the tune, change the "button" colour etc.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location
    Thank you for your answers. I lack in knowledge about excels power, I didn't know about BeforeDoubleClick. I am using this method now, it's great.

    Is there a way to execute code upon opening the file? I want to check all mp3s if they exist when I open the document.

    Edit: Also I recognized, if you use the BeforeDoubleClick method and click inbetween two cells, the script doesn't trigger. Is there a workaround for this?

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Is there a way to execute code upon opening the file
    1. There is the Workbook event Private Sub Workbook_Open()

    and click inbetween two cells, the script doesn't trigger.
    Aim better is about all I can tell you

    Paul

  10. #10
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location
    Quote Originally Posted by Paul_Hossler
    1. There is the Workbook event Private Sub Workbook_Open()

    Paul
    Just wanted to write that I found it, thanks anyways

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •