0% found this document useful (0 votes)
7 views

Step by Step With Error Handling Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Step by Step With Error Handling Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Step 1: Set Up the Excel Workbook

1. Open Excel.
2. Press Alt + F11 to open the VBA Editor.
3. In the VBA Editor, go to Insert → UserForm. A blank UserForm will appear.

Step 2: Design the UserForm

Add Controls to the UserForm

You will add various elements like text boxes, labels, and buttons.

1. Add Labels:
o From the toolbox, select the Label control and place it on the UserForm.
o Create the following labels:
 Label for "Guest Name:"
 Label for "Room Type:"
 Label for "Number of Nights:"
 Label for "Check-in Date:"
2. Add TextBoxes and ComboBox:
o Add a TextBox for "Guest Name" and set the Name property to txtGuestName.
o Add a ComboBox for "Room Type" and set the Name property to cmbRoomType.
 In the ComboBox, add the following room types in the RowSource
property: Single, Double, Suite.
o Add a TextBox for "Number of Nights" and set the Name property to txtNights.
o Add a TextBox for "Check-in Date" and set the Name property to txtCheckIn.
3. Add Buttons:
o Add a CommandButton for "Submit" and set the Caption to Submit. Set the
Name property to btnSubmit.
o Add another CommandButton for "Cancel" and set the Caption to Cancel. Set
the Name property to btnCancel.

Final Layout

Make sure your UserForm has:

 4 Labels (for Guest Name, Room Type, Number of Nights, and Check-in Date)
 3 TextBoxes (for Guest Name, Number of Nights, and Check-in Date)
 1 ComboBox (for Room Type)
 2 CommandButtons (Submit and Cancel)

Step 3: Add VBA Code for Error Handling


1. Double-click on the "Submit" button to open the code window for it.
2. Add the following code for the btnSubmit_Click() event:

vba
Copy code
Private Sub btnSubmit_Click()
On Error GoTo ErrorHandler

' Check if the guest name is empty


If txtGuestName.Value = "" Then
MsgBox "Please enter the guest's name.", vbExclamation, "Missing
Information"
txtGuestName.SetFocus
Exit Sub
End If

' Check if the room type is selected


If cmbRoomType.Value = "" Then
MsgBox "Please select a room type.", vbExclamation, "Missing
Information"
cmbRoomType.SetFocus
Exit Sub
End If

' Check if number of nights is entered and is a number


If Not IsNumeric(txtNights.Value) Or txtNights.Value <= 0 Then
MsgBox "Please enter a valid number of nights.", vbExclamation,
"Invalid Input"
txtNights.SetFocus
Exit Sub
End If

' Check if check-in date is provided


If txtCheckIn.Value = "" Then
MsgBox "Please enter a check-in date.", vbExclamation, "Missing
Information"
txtCheckIn.SetFocus
Exit Sub
End If

' If all fields are valid, show success message


MsgBox "Room booked successfully!", vbInformation, "Booking Successful"
Exit Sub

ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbCritical, "Error"
End Sub

3. Double-click on the "Cancel" button and add this code:

vba
Copy code
Private Sub btnCancel_Click()
txtGuestName.Value = ""
cmbRoomType.Value = ""
txtNights.Value = ""
txtCheckIn.Value = ""
End Sub

This code clears the form fields when the "Cancel" button is clicked.

Step 4: Test the UserForm

1. Go back to the VBA Editor and click Insert → Module. This will add a blank module.
2. Add this code to display the UserForm:

vba
Copy code
Sub ShowBookingForm()
BookingForm.Show
End Sub

3. Return to Excel.
4. Press Alt + F8, select ShowBookingForm, and click Run. The UserForm should appear.
5. Test the UserForm:
o Try leaving fields blank to see the error messages.
o Enter valid data to successfully submit the form.
o Use the "Cancel" button to clear the form.

Step 5: Save Your Work

1. Save your workbook as a Macro-Enabled Workbook (.xlsm).


2. You can also save the UserForm by going to File → Export File in the VBA Editor.

You might also like