Você está na página 1de 12

City Short courses

Input Box and Msgbox


Sub hello()
yourName = InputBox("Please enter your name)
Msgbox "Hello" & yourName
End Sub
City Short courses

With a variable
Sub hello()
Dim username As String declare variable
username = InputBox("Please enter your name")
MsgBox "Hello " & username
End Sub
City Short
courses

Variable names

Examples of invalid names


Examples of valid
are:
variables are:
4thjuly - starts with a number
City
/ - invalid character
CustomerCity
hello john - contains a space
Customer_City
Sham69
City Short courses

Declaring variables
Option Explicit
Sub DemoMacro()
Dim numWorkers As Integer
Dim salesRegion As String
Dim nextFinYear As Date
Dim Enrolled As Boolean

numWorkers = 571
salesRegion = "North West" If Option
nextFinYear = #4/1/2017# 'date note US format Explicit is at
Enrolled = True top of
module,
MsgBox "Number of Workers:" & numWorkers & vbNewLine _ variables
& "Sales Region: " & salesRegion & vbNewLine _ must be
& "Financial Year end: " & nextFinYear & vbNewLine _ Dimmed
& "Enrolment status: " & Enrolled

End Sub
City Short courses
City short courses

Simple macro to add numbers and output


result
Sub Example1()
'declare variables
Dim x as integer, y as integer, z as
integer

' assign values to variables


x = 3
y = 4
z = x + y

' output result


Msgbox z
End sub

This program is very limited because the numbers are hard-


coded (fixed within the program). You could read in a
number from an input box.
City Short courses

Macro to input a number, double it and


output result
use InputBox to get input
Sub EnterNumbers ()

Dim num as Integer


num = InputBox("Enter number under 5000", _
"Enter numeric data")
num = num * 2
MsgBox "The number multiplied by two is " & num

End Sub
City Short courses

Double number in a cell


read and write from/to cells
Sub readWriteCells ()
num = Range(A1).Value
num = num * 2
Range(B1).Value = num
MsgBox A1 times 2 is & num
End Sub

Sub readWriteCells2 () shows you dont need a variable


Range(B1).Value = Range(A1).Value * 2
End Sub
City Short courses

Arithmetical operators
Operator Operation Example Answer

+ Add 5+4 9
- Subtract 7-2 5
* Multiply 3*5 15
/ Divide 10/4 2.5
\ N1 \ N2 - integer division 15\4 3
Mod X Mod Y returns remainder 10 mod 3 1

^ X ^ Y gives the value of X raised to the power of Y 2^3 8

You are more likely to be using variables or range references than literal
numbers:
z=x+y
x=x+y
Range(C3).value = Range(D3).value Range(E3).Value
Activecell.value = activecell.offset(0,1).value /
activecell.offset(0,2).value
City Short courses

Numeric variable types


VBA type Range of Values
Byte 0 to 255

Integer -32,768 to 32,767

Long -2,147,438,648 to 2,147,438,647

Single -3.402823E38 to -1.401298E-45 for negative values;


1.401298E-45 to 3.402823E38 for positive values

Double -1.79769313486231E308 to -4.94065645841247E-324


for negative values;
4.94065645841247E-324 to 1.79769313486232E308
for positive values

Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807


City Short courses
City short courses

Non-numeric data types

Boolean True or False


Date (in US format)
String Alphanumeric characters
Object Any Object reference
Variant can be object or data type
City Short courses

Variant is default type


Dim variableName
If no variable type specified, variable is of type Variant
Dim variableName as Variant
Same as above

Dim x, y as Integer
Declares x as a variant, y as an integer
City Short courses

Concatenate operator
The concatenation operator is & (Shift+7). For example, you can
use it to concatenate a string to a number:
MsgBox "You will be paid " & payrate & " pounds per hour.

There is also + which can only concatenate one string to another


"Manchester " + "United

The following line would cause a run-time error:


MsgBox "You will be paid " +payrate +" pounds per hour.

You cannot use the + operator to concatenate a number to a


string