2.2.1 Numeric Data Types
2.2.1 Numeric Data Types
2.2.1 Numeric Data Types
Numeric data types are types of data that consist of numbers. We can
compute the numeric data mathematically with various standard operators
such as plus, subtract, multiply, divide and so on. In Excel VBA, we can
divide the numeric data into 7 types, as shown in Table 2.2
________________________________________________________
Private Sub CommandButton1_Click( )
Dim StudentName(1 to 5) As String
For i = 1 To 5
StudentName(i) = InputBox("Enter student Name")
Cells(i, 1) = StudentName(i)
Next
End Sub
_______________________________________________________
^
Exponential MsgBox 2^4 gives a value of 16
*
Multiplication MsgBox 4*3 gives a value of 12,
/
Division MsgBox 12/4 gives a value of 3
Integer Division(discards
\
the decimal places) MsgBox 19\4 gives a value of 4
Meaning Example
Operator
MsgBox 2<3 returns true while MsgBox 4>5 returns
<
Less than false
<=
Less than or equal to MsgBox 3<=4 returns true
>
Greater than MsgBox 5>4 returns true
=
Equal to MsgBox 10=10 returns true
<>
Not Equal to MsgBox 9<>10 returns true
Meaning Example
Operator
If A>=80 And B<101
And
Logical Conjunction thenGrade=”A”
If income>5000 or car>2
Or
Logical Disjunction thenStatus=”Rich”
Similar to Or, except that it returns False if MsgBox 4 > 3 Xor 5 >2
Xor
both camparison values are true returns false
A sub procedure begins with a Sub statement and ends with an End Sub
statement. The program structure of a sub procedure is as follows:
Example 5.1
In this example, a sub procedure ResizeFont is created to resize the font in
the range if it fulfils a value greater than 40. There are two parameters or
arguments associated with the sub procedure, namely x for font size and Rge
for range. This sub procedure is called by the event procedure Sub
CommandButton1_Click () and passed the values 15 to x (for font size) and
Range (“A1:A10”) to Rge (for range) to perform the task of resizing the font
to 15 for values>40 in range A1 to A10.
To make the program more flexible and interactive, we can modify the above
program to accept input from the user. The values input by the user through
the input boxes will be passed on to the procedure to execute the job, as
shown in Example 20.2.
Example 5.2
Private Sub CommandButton1_Click()
Dim rng As String
rng = InputBox(“Input range”)
x = InputBox(“Input Font Size”)
ResizeFont x, Range(rng)
End Sub
In Excel VBA, a function is similar to a sub procedure but the main purpose
of the function is to accept a certain input from the user and return a value
which is passed on to the main program to finish the execution. There are two
types of functions, the built-in functions (or internal functions) and the
functions created by the programmers, or simply called user-defined
functions. The first built-in function is the Message Box. The message box
acts as a dialog box where users can interact with the computer, it is able to
perform certain actions in response to what the user clicks or selects.
End Sub
By clicking the first message box, you will see the message as shown in
Figure 5.2
Figure 5.3
Now double-click on the second button and enter the following code:
Private Sub CommandButton2_Click()
End Sub
Click on the button and you shall see the following dialog. You will notice
the Yes, No, Cancel buttons:
Figure 5.4
Now double-click on the second button and enter the following code:
Click on the button and you would notice that the displayed message box
comprises only the Yes and No button, as shown in Figure 5.4
An InputBox( ) function displays a message box where the user can enter a
value or a message in the form of text. The syntax is
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)
or
Now enter the function CubeRoot just like you enter the formula of MS
Excel, as shown in Figure 5.3. The value of cube root for the number in cell
C4 will appear in cell D4.