70 likes | 92 Views
Visual Basic for Excel : Review. Type of variables, expressions, giving value to a variable. Basic types : Integer (%) Double(# ) Single String($) Boolean Definition of types is not obligatory! (but very useful…)
E N D
Visual Basic for Excel : Review Type of variables, expressions, giving value to a variable Basic types: Integer(%) Double(#) Single String($) Boolean Definition of types is not obligatory! (but very useful…) Basic operations: + - * / \ ^ (arithmetical) And Or Not (logical) Relations: = < > <= >= <> Arithmeticalexpression: a*a*a*a - 81 Logical expression: fa*fm<0 Giving value: variable = expression like: fa = a^4 - 81 Sub <name>( ) <definition of types (optional)> <statements> End Sub Structure of a VBA program:
Visual Basic for Excel – conditional statements give: X ,write: X yes X is even ? Y = X*X no write: Y IF <log.expr.> THEN<statement> Results:(2,4);(-4,16 );(3,16 ); (–1,16 ) If – Then and If – Then – Else statements The values of X are (in both cases): 2 , -4 , 3 , -1 give: X ,write: X yes X is even ? Y = X*X no Y = X + 8 write: Y IF<log.expr.> THEN <statement1> ELSE<statement2> Results:(2,4);(-4,16);(3,11);(-1,7)
entrance-testing loop Loops start start give: n k=1 give: n k=1 no k<=n ? vége give: Name, E1 , E2 yes give: Name, E1 , E2 Ave=(E1+E2)/2 write: Name , Ave Ave=(E1+E2)/2 write: Name , Ave k=k+1 yes k<=n ? k=k+1 no • exit-testing loop stop Example: the average of two exams for n students What about if n=0 ?
Loops start give: n k=1 give: Name, E1 , E2 Ave=(E1+E2)/2 write: Name , Ave k=k+1 yes k<=n ? no • exit-testing loop stop Do - Loop While Example: the average of two exams for n students VBAcode n=InputBox(“n=?”): k=1 Do Name=InputBox(“Name=?”) E1=InputBox(“E1=?”) E2=InputBox(“E2=?”) Ave=(E1+E2)/2 : Cells(k,1)=Name Cells(k,2)=Ave : k=k+1 LoopWhile k<=n
entrance testing loop Loops start give: n k=1 no k<=n ? vége yes give: Name, E1 , E2 Ave=(E1+E2)/2 write: Name , Ave k=k+1 Do While - Loop Example: the average of two exams for n students Visual Basic code n=InputBox(“n=?”): k=1 DoWhilek<=n Name=InputBox(“Name=?”) E1=InputBox(“E1=?”) E2=InputBox(“E2=?”) Ave=(E1+E2)/2 : Cells(k,1)=Name Cells(k,2)=Ave : k=k+1 Loop
Loops entrance testing loop start Visual Basic code give: n k=1 n=InputBox(“n=?”) FOR k=1 TO n Name=InputBox(“Name=?”) E1=InputBox(“E1=?”) E2=InputBox(“E2=?”): Ave=(E1+E2)/2 Cells(k,1)=Name : Cells(k,2)=Ave NEXTk no k<=n ? vége yes give: Name, E1 , E2 Ave=(E1+E2)/2 write: Name , Ave k=k+1 ForTo - Next Example: the average of two exams for n students
VBA – summary of the statements we know yes ? no yes no ? no ? yes yes ? no Giving value, input, output a=1: b= InputBox(„b?”, , 2) : c=cells(1,1) D=b^2-4*a*c : cells(2,2)=(-b+sqr(D))/(2*a) Sequential statements Conditional statements Loops For – Next Do - Loop Go To <label>