In Microsoft Visual Basic for Applications (VBA), the code for a "save" button would typically include instructions for saving the data entered in the form to a specified location, such as a file or database. Here is an example of VBA code for a save button that saves the data in a form to a new Microsoft Excel worksheet:
Save Button Code
Save Button / Submit Button
Private Sub SaveButton_Click() 'Declare variables to store the data from the form Dim name As String Dim age As Integer Dim gender As String
' Get the data from the form's text boxes name = Me.NameTextBox.Text age = Me.AgeTextBox.Text gender = Me.GenderTextBox.Text
'Create a new worksheet in the active workbook Dim ws As Worksheet Set ws = ThisWorkbook.Sheets.Add
' Add the data to the new worksheet ws.Cells(1, 1).Value = "Name" ws.Cells(1, 2).Value = "Age" ws.Cells(1, 3).Value = "Gender" ws.Cells(2, 1).Value = name ws.Cells(2, 2).Value = age ws.Cells(2, 3).Value = gender
' Save the workbook ThisWorkbook.Save End Sub
This code assumes that the form has text boxes named "NameTextBox",
"AgeTextBox", and "GenderTextBox", and that the button is named "SaveButton".
When the button is clicked, the code retrieves the data from the text boxes
and stores it in variables. It then creates a new worksheet in the active
workbook, adds the data to the worksheet, and saves the workbook.
2nd Example:
To create a "Save" button in a UserForm in VBA, you can use the following
code:
Private Sub cmdSave_Click() 'Code to save the form data goes here 'For example:
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = Me.txtName.Value ThisWorkbook.Sheets("Sheet1").Range("A2").Value = Me.txtAddress.Value MsgBox "Data saved successfully!"
End Sub
This code will save the data entered in the "txtName" and "txtAddress" TextBoxes on a UserForm to cells A1 and A2 on Sheet1 of the current workbook. The last line of the code.
Tags:
Save Button