How to Write a Code of VBA with Simple Project ?

How to Write a Code of VBA with Simple Project ?

Example:

 

To create a UserForm in VBA Excel, you will need to follow these steps: 

1. Open the Visual Basic Editor (VBE) by pressing the "Alt + F11" keys.
2. In the VBE, go to the "Insert" menu and select "UserForm." This will create a new userform in the project window.

 

Userform1

 

3. To add controls to the userform, such as text boxes and buttons, go to the "Toolbox" on the left side of the VBE and select the desired control. Then, click on the userform to place the control.

4.To modify the properties of the controls, such as the name and caption, select the control and go to the "Properties" window on the right side of the VBE.

5. To add code to the userform, double-click on the userform in the project window to open the code window. You can then add event-handling code for the controls, such as what happens when a button is clicked.

6. Once you have finished designing the userform, you can run it by going to the "Run" menu and selecting "Run UserForm."

 

To Write a Code of Save Button In VBA (Visual Basic Application)

To Change the Caption of Command Button name to cmdSaveBtn. Than Double Click to the Save Button. 
 
Follow the Step by Step
 
1. Show the this code 
 
Private Sub save_Click()
 
End Sub  
 
2. Your Excel sheet name is sheet1 and your data is place to 3 no column means "C" Column. 
 
3. Write the code of showing the below. 
 
Private Sub save_Click()
 
'Your code for saving the data here
 
Dim lastRow As Long

    'Find the last row of data in column C
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row
    
    'Save the data from Textbox1 to Sheet1, range C3 to the last row of column C
    Sheets("Sheet1").Range("C" & lastRow + 1).Value = UserForm1.TextBox1.Value

    'Clear the Textbox after saving the data
    UserForm1.TextBox1.Value = ""
    End If

End Sub
 
 
Note: IF your Textbox is Blank than Save is show the Error. Like "Please enter a value in Textbox before saving."
 
Place the Code of After the  "Private Sub save_Click()"
 
 
If TextBox1.Value = "" Then
        MsgBox "Please enter a value in Textbox before saving."
Else 
 
 
 

Only Numeric Allow the Textbox Follow the Process. 
 
1. Double Click on the Textbox1 
2. Change the Change to KeyPress on Top-Right Side.
3. Place the code of following the below 
 
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 
'Restriction of Alphabet Only Number Allow   
 
 If Not IsNumeric(Chr(KeyAscii)) Then
        KeyAscii = 0
 End If
 
 

Previous Post Next Post