1 / 7

Basic types : Integer (%) Double(# ) Single String($) Boolean

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…)

tmcferren
Télécharger la présentation

Basic types : Integer (%) Double(# ) Single String($) Boolean

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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:

  2. 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)

  3. 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 ?

  4. 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

  5. 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

  6. 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

  7. 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>

More Related