Search code examples
excelvbacommandbutton

Multiple command buttons in sequence with one another


I am new to programming and I want to create an application for basketball coaches to be able to keep stats of their players by using VBA in Excel with command buttons.

Instead of manually inputting stats for each player in a game, I want to be able to click a command button with a players name on it to select that player's row. Then click a button that will have an action on it and input a number.

I will have 12 buttons for the players and 40 buttons for the actions (e.g. pts, reb,stl,etc.)

For example: When I click the player's name button, it will select the row that the player's attribute are in. Then when I select the action (e.g. points), it will add the number 2 to the column labeled points.

I want to use the action buttons for all 12 players so only put the number in when i click the player's name button. So the "pts" button will work for all 12 player buttons. Overall, I want to make a stat sheet for the coaches with command buttons instead of moving the cursor and inputting the information in manually.

Any suggestions on how to do so? Thank you in advance.

Clancy


Solution

  • Some example code, using a module-scoped variable to store which player is being processed, might be:

    Option Explicit
    
    Private CurrentPlayerRow As Long
    
    Sub PlayerA_Click()
        CurrentPlayerRow = 3
    End Sub
    Sub PlayerB_Click()
        CurrentPlayerRow = 4
    End Sub
    Sub PlayerC_Click()
        CurrentPlayerRow = 5
    End Sub
    
    Sub Action1_Click()
        'Update column D by adding 2 to the cell value
        Cells(CurrentPlayerRow, "D").Value = Cells(CurrentPlayerRow, "D").Value + 2
    End Sub
    Sub Action2_Click()
        'Update column G by adding an inputted number to the cell value
        Cells(CurrentPlayerRow, "G").Value = Cells(CurrentPlayerRow, "G").Value + CLng(InputBox("Enter a number:"))
    End Sub
    

    (Not knowing anything about basketball scoring and/or statistics, I wasn't sure what sort of actions you wanted to process.)