Previous  Home  Next

Basic Basics

This is the code and comments from the "Basic Basics" section of the seminar (mod01BasicBasics)

Option Explicit

Sub BasicBasics_1()
' Comments, Variables
' This is a comment
' VBA/PowerPoint ignores it but we humans can use it to remind
' ourselves what we were thinking when we wrote that code
' You WILL have a "What was I THINKING???" moment sooner or later.
' Use LOTS of comments.

' Normally, we indent code to make it easier to read, so ...

    ' Variables are handy slots to store information we want to use later
    ' To create them, we have to "Dim" them
    ' Dim is short for "Dimension"
    ' When we Dim a variable, we tell VBA its name and what type of
    ' information it will contain

    ' Different types of variables can hold text, numeric, true/false or
    ' several other types of information.

    ' GOOD PROGRAMMING PRACTICE:
    ' Always Dim variables as the specific type for the data they'll hold
    ' Always give them a name that reflects that data type
    ' For example:

    ' Text is also called "Strings" in VBA
    ' String variables store text
    ' We use str or s as a prefix to identify string variables:

    Dim strMyName As String

    ' Now VBA knows that we want to store some text in a variable
    ' called strMyName.  It won't let us store other types of data there
    ' by mistake.

    ' Other data types that variables can hold (and their common prefix):
    ' Integer (i or int)
    ' Long Integer or Long (l or lng)
    ' Boolean (true/false) (b, bl or bool)
    ' Object (o)

    ' Now let's store some data in strMyName:
    strMyName = "Steve Rindsberg"

    ' And do something with it:
    MsgBox (strMyName & " welcomes you to The Mysteries of VBA")

End Sub

Sub BasicBasics_2()
    ' Getting input from the user
    ' If/Then logical decisions
    ' While/Wend loops
    ' Debugging

    ' You might need to get a simple Yes/No decision from the user
    ' or you might want to get an answer that you can't predict
    ' in advance.

    ' You can use MsgBox to get simple decisions:
    ' First, DECLARE (Dim) a variable to hold the answer
    ' MsgBox returns a Long, so that's what you should declare:

    Dim lngAnswer As Long

    ' Now ask your question and request a Yes/No answer:
    lngAnswer = MsgBox("Are you with me so far?", vbYesNo, _
        "Help us help you ...")

    ' lngAnswer now holds a value that tells us whether the user
    ' chose Yes or No
    ' We have to decide what to do based on that:
    If lngAnswer = vbYes Then
        ' they understood everything so far
        ' pat on the back:
        MsgBox ("I'm PROUD of you!")
    Else
        ' uh-oh - problems
        ' let's find out more ...
        ' this time we'll get more generic information
        ' using InputBox
        ' we need someplace to hold the info so:
        Dim strMoreInfo As String

        strMoreInfo = ""    ' make sure it's blank to start

        ' ask what the problem was and make sure we get an answer
        ' no blanks allowed!

        ' We'll keep asking until they type in SOMETHING
        While strMoreInfo = ""

            strMoreInfo = InputBox("Terribly sorry!  What didn't you understand?", _
                "Help us help you more")

            ' did we get any useful information?
            If strMoreInfo = "" Then   ' they didn't tell us anything - scold them
                MsgBox ("Please type something, anything." _
                    & vbCrLf _
                    & "Click OK to try again.")
            Else    ' they were good computer users so be nice:
                ' and reply politely:
                MsgBox ("See Steve in the Help Center later.  He'll be happy to" _
                    & " help you with " _
                    & strMoreInfo)
            End If

        Wend

    End If

    ' After we run through that a couple times normally,
    ' we'll go back and do it step-by-step
    ' and learn a few basic debugging tricks
    ' Compile before running
    ' Stepping through the code
    ' Setting breakpoints
    ' Debugging messages

End Sub

Click Next to continue

Previous  Home  Next