210 likes | 317 Views
Lab 01 Forms in excel. In The Name Of Allah. 1. Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’ properties. 5. Write code. 6. Use Show method to display the form. objectives. VBA : • Standard programming language for office
E N D
Lab 01 Forms in excel TahaniALdweesh In The Name Of Allah
1. Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’ properties. 5. Write code. 6. Use Show method to display the form. Tahani ALdweesh objectives
VBA : • Standard programming language for office programs . • Can be used in any office applications in same way . • As you do more work with VBA you will need to use VBE(Visual Basic Editor ), TahaniALdweesh VBA Visual Basic For Application
:The Visual Basic Editor (VBE) is the environment in which you work with VBA programming code. TahaniALdweesh Visual Basic Editor
TahaniALdweesh -Open Microsoft Excel - Tools > Macro > Visual Basic Editor (2003) ALT + F11 .user form < -insert
TahaniALdweesh • The Visual Basic Editor windows Project window User Form Work Area properties window
Project window: shows a hierarchical view of the contents of the current VBA project view project explorer or Ctrl+R Properties window: shows the various properties available for whatever object that is highlighted view Properties window or F4 User form window : the design area ,the toolbox will appear by default insert user form Code window: where the procedures take place view code or F7 Work area: the rest of the VBE TahaniALdweesh
Properties are the physical descriptions of all Excel objects, and the description of the particular property is called the value of the property. Contains detailed information about any selected part of a project in .the Project Explorer TahaniALdweesh Properties Window
TahaniALdweesh ToolBoxs ToolBox
• Label: used to instruct the user • Textbox: enables the user to enter textual info. • List box: list of choices. • Checkbox: allow you to choose more than one option. • Combo box: is so named because it combines the function of textbox and a list box. • Option buttons: restricts the user to one choice. • Frame: used for: 1-groups option buttons 2-visually, arranging different portions of the form TahaniALdweesh
In term of VB, variablesare areas allocated by the computer memory to hold data. Like the mail boxes, each variablemust be given a name.To name a variable in Visual Basic, you have to follow a set of rules. Tahani ALdweesh Vb code
variablesThe following are the rules when naming the :in Visual Basic • It must be less than 255 characters. • Must start with a letter (a to z, A to Z). • It must not begin with a number. • No spacing is allowed . • Can contain any number of letters or digits (a digit is 0 to 9). • Can contain the underscore. TahaniALdweesh Variable Names
TahaniALdweesh Examples of valid and invalid variablenames
Declaring Variables In Visual Basic, one needs to declare the variables before using them by assigning names and data types. They are normally declared in the general section of the codes' windows using the Dim statement. The format is as follows: DimVariable Name As Data Type TahaniALdweesh Vb code
Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date • You may also combine them in one line , separating each variable with a comma, as follows: Dim password As String , yourName As String, firstnum As Integer TahaniALdweesh Example
Constants are different from variables in the sense that their values do not change during the running of the program. Declaring a Constant ConstConstant Name As Data Type = Value :• Example Const Pi As Single=3.142 Const Temp As Single=37 Const Score As Single=100 TahaniALdweesh Constraints
After declaring various variables using the Dim statements, we can assign :values to those variables. The general format of an assignment is Variable=Expression • The variable can be a declared variable or a control property value. • The expression could be a mathematical expression, a number, a string, a Boolean value (true or false) and more. TahaniALdweesh Assigning Values to Variables
firstNumber=100 secondNumber=firstNumber-99 userName="John Lyan" userpass.Text = password Label1.Visible = True Command1.Visible = false Label4.Caption = textbox1.Text ThirdNumber = Val(usernum1.Text) total = firstNumber+secondNumber+ThirdNumber TahaniALdweesh Examples:
TahaniALdweesh Circle aRea Program :
Private Sub CommandButton1_Click() Dim rad As Double rad = Val(TextBox1.Text) Dim area As Double area = 3.17 * rad * rad TextBox2.Text = area End Sub Private Sub CommandButton2_Click() UserForm1.Hide End Sub TahaniALdweesh The Code is :
TahaniALdweesh Any Question