Forum Discussion

amarisisabel's avatar
amarisisabel
Copper Contributor
Dec 06, 2023

Creating Command Buttons

Hello,

 

I am wanting to enter daily productivity metrics per employee and create a command button that will transfer the data on a daily basis to then visit the cumulative productivity for the department. Can someone help me with what code I need to enter to make this possible?

 

Thank you in advance for your help!

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    amarisisabel 

    To achieve this, you can use VBA (Visual Basic for Applications) to create a command button that, when clicked, transfers the data from the daily sheet to the cumulative sheet. Below is a simple example to get you started:

    1. Insert a Command Button:

    Go to the "Developer" tab. If you don't have the Developer tab visible, you can enable it in Excel options.

    In the Developer tab, click on "Insert" in the Controls group.

    Choose the "Button (ActiveX Control)" option.

    Click on your worksheet to place the button and draw it.

    2. Assign Macro to the Button:

    Right-click on the button and choose "Properties."

    In the Properties window, you can change the button's name (e.g., btnTransfer) and caption (e.g., "Transfer Data").

    Close the Properties window.

    3. Open VBA Editor:

    Right-click on any sheet tab and select "View Code."

    4. Write VBA Code:

    In the VBA editor, you'll see the code window for the worksheet. Enter the following code:

    Vba code (is untested and should serve as a draft):

    Private Sub btnTransfer_Click()
        ' This code transfers data from the daily sheet to the cumulative sheet
        
        Dim dailySheet As Worksheet
        Dim cumulativeSheet As Worksheet
        Dim lastRowDaily As Long
        Dim lastRowCumulative As Long
        
        ' Set references to the sheets
        Set dailySheet = ThisWorkbook.Sheets("DailySheet") ' Change to the actual name of your daily sheet
        Set cumulativeSheet = ThisWorkbook.Sheets("CumulativeSheet") ' Change to the actual name of your cumulative sheet
        
        ' Find the last row with data in the daily sheet
        lastRowDaily = dailySheet.Cells(dailySheet.Rows.Count, "A").End(xlUp).Row
        
        ' Find the last row with data in the cumulative sheet
        lastRowCumulative = cumulativeSheet.Cells(cumulativeSheet.Rows.Count, "A").End(xlUp).Row
        
        ' Copy data from the daily sheet to the cumulative sheet
        dailySheet.Range("A2:C" & lastRowDaily).Copy cumulativeSheet.Range("A" & lastRowCumulative + 1)
        
        MsgBox "Data transferred successfully!", vbInformation
    End Sub

    Close the VBA editor.

     

    Now, whenever you click the button, it will transfer data from the "DailySheet" to the "CumulativeSheet" in the specified columns (adjust the column letters as needed). Remember to replace "DailySheet" and "CumulativeSheet" with the actual names of your sheets.

    This is a basic example, and you might need to adjust it based on the layout of your sheets and the specific columns you want to transfer. The text and steps were edited with the help of AI.

     

    If this doesn't help, I recommend adding more information. Information such as Excel version, operating system, storage medium, etc. This would help immensely to provide you with a more concrete solution suggestion.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources