Sie sind auf Seite 1von 5

Training handout, FCl Lukavac, Febr. 2012, Msd, eng.

P Ratkovic

Ms Excel
And basic techniques of
Visual Basic programming
Training handout

Part 1:
MS Office 2007+

Start/ExcelOption/Popular/Show Developer Tab in the Ribbon (Check it)

MS Office 2003 (XP)

View/ToolBar/ ControlToolBox (Check)


Forms (Check)
Exit Design Mode (Check)

Alt+F11 Visual Basic editor

Part 2:
Program code is a set of commands and instructions, which
programmers use to describe and solve a logical task.
Spelling (grammar) are words and rules which programmer use to write
program code.
A good program spelling and code have the following characteristics:
1. Code must correctly solve logical task,
2. The code should be as short as possible, with as few written
commands,
3. To Be clearly printed,
4. To have enough description and notes (remarks) REM.

1
Training handout, FCl Lukavac, Febr. 2012, Msd, eng. P Ratkovic

Private Sub CommandButton1_Click()

'This is my first VBA code

Cells(1, 1) = 878
Cells(2, 1) = "Marry"

End Sub

X=3

To numerical variable which has signed X is added numerical value of 3.

Name$ = Marry

To textual variable which has sign name$ is added textual value (word)
Marry.

Numerical operation:
Calculation:
Logical
X=3+2 X=5 operation with
X=32 X=1 numbers:
X=3*2 X=6 = <=
X=3/2 X=1.5 > And
X=3^2 X=3*3=9
< Or
X = Int (3.52) X=3 cutting off
X = Round (3.52) X=4 rounding >=
X = Rnd 0 < X < 1 Random value

2
Training handout, FCl Lukavac, Febr. 2012, Msd, eng. P Ratkovic

Operation with textual variables (STRING, $,):

Name$ = Marry & , Tony


Cells(1,1) = Name$
Cells(2,1)= Left(Marry , 2)
Cells(3,1)= Right(Marry, 2) Excel sheet
Cells(3,1)= Mid(Marry , 2,3) A B
UCase$(Marry)=MARRY
LCase$(Marry) =marry Marry,
Tony
Ma
ry
CELLS
A B
arr C D
Cells(1, Cells(1, Cells(1, Cells(1,
Cells(RowIndex, ColumnIndex) 1
1) 2) 3) 4)
Cells(2, Cells(2, Cells(2,
Cells(3,1) = Cells(1,1) + Cells(2,2) / 3 2
1) 2) 3)
Cells(3, Cells(3,
3
1) 2)
X=X+1 Cells(4, Cells(4,
4
1) 2)
IFTHEN

If(true) . Then(to be done).Else (if not true_to be done)


If x>3 Then
Name$=Tony
IF logical statementis true THEN it is to be done next Else
Name$=Marry
IF X>3 THEN Name$=Tony End If
If x>3 Then If x>3 Then
Name$=Tony Name$=Tony
End If ElseIf X<3 Then
Name$=Marry
Else
Name$=
End If
FORNEXT (loop construction)

from i+1 to n do ....


For i=1 To n

This code will be done n times


Next i

3
Training handout, FCl Lukavac, Febr. 2012, Msd, eng. P Ratkovic

If i=n then exit loop else gop back to next loop for next i loop as (i=i+1)

'Counter after code


Conditional loop exit.
i=1
Do For i=1 To n
Cells ( i , 1 ) = 2 * i X=X+i
i=i+1 If x>3 then Exit For
Next i
Loop Until i > 5
Do..Loop code
'Counter before code
i=0 i=1
Do Do
i=i+1 Cells ( i , 1 ) = 2*I
Cells ( i , 1 ) = 2 * i i=i+1

Loop Until i =5 Loop Until i >5

Conditional jump or redirection of code foreword to some specifies point in the code
written as label with : in this example label named Corn

i=0
Do
i=i+1
Cells ( i , 1 ) = 2 * i

If i=5 Then GoTo Corn

Loop Until i =5

Corn:

4
Training handout, FCl Lukavac, Febr. 2012, Msd, eng. P Ratkovic

Conditional jump or redirection of code backward to label named Corn

Corn:

i=0
Do
i=i+1
Cells ( i , 1 ) = 2 * i

If i=5 Then GoTo Corn

Loop Until i =5

'Simulation of Do..Loop and For .. Next code construction with command GoTo

i=0

Corn:

i=i+1

Cells ( i , 1 ) = 2 * i

If i < 5 Then GoTo Corn

End of the first part.