Presentation is loading. Please wait.

Presentation is loading. Please wait.

VBA Programming Functions and Decisions. If-Then-Else Structure used to consider alternatives If then --or-- If then Else End If.

Similar presentations


Presentation on theme: "VBA Programming Functions and Decisions. If-Then-Else Structure used to consider alternatives If then --or-- If then Else End If."— Presentation transcript:

1 VBA Programming Functions and Decisions

2 If-Then-Else Structure used to consider alternatives If then --or-- If then Else End If

3 If-Then Decision Flow Flow chart branches forward Code: vName = Inputbox(…) If vName = “” then vName = Inputbox(…) End If Range(“B3”) = vName Start Get user name Is name empty? Write name to cell B3 End Get user name Yes No

4 If-Then-Else Structure Selects one of two choices Code If [B3].value < 0 then [B3].font.colorindex = 3 Else [B3].font.colorindex = 41 End If Is value negative? Set text color to black Yes No Set text color to red

5 If-Then-Else Example

6 MsgBox Function MsgBox(Prompt, Buttons, Title) Buttons is a number telling VBA which buttons to show MsgBox(“Is it raining?”, 4, “Raining”) yields:

7 MsgBox Function How do you know the button numbers? Use Help to find them Use built-in constants MsgBox(“Is it raining?”, vbYesNo) VBA finds that vbYesNo = 4 Results are the same Returns a number Yes = 6 (vbYes) No = 7 (vbNo) The MsgBox function cannot appear by itself.

8 MsgBox Function Example Include in an assignment statement Use “If-Then” to take action: vAnswer = MsgBox(“Is it raining?”, vbYesNo) If vAnswer = vbYes then vOut = “Help, its raining!” Else vOut = “OK, its dry!” End if MsgBox vOut


Download ppt "VBA Programming Functions and Decisions. If-Then-Else Structure used to consider alternatives If then --or-- If then Else End If."

Similar presentations


Ads by Google