IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

18
IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh

Transcript of IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Page 1: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

IN THE NAME OF ALLAH

UserForms on VBALab 06

Tahani Al_dweesh

Page 2: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Objectives

-Using offset() method.-Work more with ranges.

-Example on UserForm.

Tahani Al_dweesh

Page 3: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Using the offset method Range object has an Offset property

that can be useful when wants to move the active cell around .

For example , you might need to refer to the cell that’s two rows down and one column to the right of the active cell ,the offset method can do that.

  Tahani Al_dweesh

Page 4: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Rowoffset : the number of rows to offset range ,use positive number to move down , negative number to move up.

Columnoffset : the number of columns to offset range , use positive number to move right , negative number to move left.

Tahani Al_dweesh

Range ( ) .offset ( Rowoffset , Columnoffset )

Page 5: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Tahani Al_dweesh Reham AlMukhallafi - IS 424

Example :

ActiveCell.Offset (1,0) = 1 

Place a "1" one row under the

Active cell (E6)

Page 6: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

UserForm

-To make it easier for users to enter data in a workbook, you can create an Excel UserForm .

-When the message box is not sufficient any more to communicate with the user you need to start developing userforms.

Tahani Al_dweesh

Page 7: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

The form is used to require values, parameters and information from the user to feed the VBA procedure .

Different basic controls: can be added to the userform they are called :

LabelTextBoxComboBoxListBoxCheckBoxOptionButton ,Frame,CommandButton,SpinButtonImage

Tahani Al_dweesh

Page 8: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Set up the worksheet

In this example:User will enter data to fill information about his course, by opening the Excel UserForm, and filling the text box, and clicking a button.

Tahani Al_dweesh

Page 9: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Create an Excel UserForm

1- Alt+F11 to

open Visual Basic Editor.

2 -Insert | UserForm

Tahani Al_dweesh

Page 10: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Add Controls to the Excel UserForm

Tahani Al_dweesh

Page 11: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

1 -Initialising the Form

View -> Code / or Enter F7-From Object menu Choose

UserForm-From Procedure menu choose

Initialize

Tahani Al_dweesh

Page 12: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Tahani Al_dweesh

Private Sub UserForm_Initialize() txtName.Value = "" txtPhone.Value = "" With cboDepartment .AddItem "Sales" .AddItem "Marketing" .AddItem "Administration" .AddItem "Design" .AddItem "Advertising" .AddItem "Transportation" End With cboDepartment.Value = "" With cboCourse .AddItem "Access" .AddItem "Excel" .AddItem "PowerPoint" .AddItem "Word" .AddItem "FrontPage" End With cboCourse.Value = "" chFullTime = FalseEnd Sub

Page 13: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Tahani Al_dweesh

Private Sub cmdOK_Click() Worksheets("Course Bookings").Activate Range("A1").Select Do If IsEmpty(ActiveCell) = False Then ActiveCell.Offset(1, 0).Select EndIf Loop Until IsEmpty(ActiveCell) = True ActiveCell.Value = txtName.Value ActiveCell.Offset(0, 1) = txtPhone.Value ActiveCell.Offset(0, 2) = cboDepartment.Value ActiveCell.Offset(0, 3) = cboCourse.Value

Page 14: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Tahani Al_dweesh

If optIntroduction = True Then ActiveCell.Offset(0, 4).Value = "Intro" Else If optIntermediate = True Then ActiveCell.Offset(0, 4).Value = "Intermed" Else ActiveCell.Offset(0, 4).Value = "Adv" End If If fullTime = True Then ActiveCell.Offset(0, 5).Value = "Yes" Else ActiveCell.Offset(0, 5).Value = "No" End IfRange("A1").SelectEnd Sub

Page 15: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Add code to the buttons

Tahani Al_dweesh

Private Sub cmdClearForm_Click() Call UserForm_InitializeEnd Sub

Private Sub cmdCancel_Click() Unload MeEnd Sub

Page 16: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Test the Excel UserForm

Tahani Al_dweesh

Page 17: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Create a Button to open the Excel UserForm

Tahani Al_dweesh

Sub OpenCourseBookingForm() frmCourseBooking.ShowEnd Sub

Page 18: IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.

Any Question?

Tahani Al_dweesh