You are on page 1of 12

'This makes Excel a B**ch !

'This ensures all variables must be defined


'(This is performed by a DIM statement)
Option Explicit

'This assigns a value to the Excel Cell D7


'The value is the text "Hello World!"
Cells(7, 4) = "Hello World!"

'This assigns a value to the ColorIndex property


'of the Interior property of the Excel Cell D7
'The value is the number 3
' (Sets the background color red)
Cells(7, 4).Interior.ColorIndex = 3

'This assigns a value to the variable currentColor


'The value is the value of the Excel Cell E7
currentColor = Cells(7, 5)

'This assigns a value to the variable currentColor


'The value is the value of the variable
'currentColor + 1
currentColor = currentColor + 3

'This assigns a value to the Excel E7


'The value is the value of the variable currentColor
Cells(7, 5) = currentColor

'This assigns a value to the ColorIndex property of the


'Interior property of the Excell E7.
'The value is the value of the variable currentColor
Cells(7, 5).Interior.ColorIndex = currentColor

'This assigns a value to the variable someText


'The value is the text "King James"
someText = "King James"

'This assigns a value to Excel Cell B10


'The value is the value of the variable someText
Cells(10, 2) = someText

'This assigns a value to the variable someText


'The value is the number 1969
someText = 1969
'This assigns a value to Excel Cell B11
'The value is the value of the variable someText
Cells(11, 2) = someText

'This assigns a value to the variable someText


'The value is the text 1969
someText = "1969"

'This assigns a value to Excel Cell B12


'The value is the value of the variable someText
Cells(12, 2) = someText

'This assigns a value to the variable Comment


'The value is the return value from InputBox,
'entered by the user
Comment = InputBox("Please enter your comment")

'This assigns a value to the Excel cell A1


'The value is the value of the variable Comment
Cells(1, 1) = Comment

'This assigns a value to the Excel cell A2


'The value is the text "Comment"
Cells(2, 1) = "Comment"

'This assigns a value to the Range of cells C3:F6


'The value is the value of the variable Comment
Range("C3:F6") = Comment

'This assigns a value to the Excel cell C1.


'The value is the value of the Excel cell C1
'concatenated with " > " concatenated with the
'value of the variable Comment
Cells(1, 3) = Cells(1, 3) & " > " & Comment

'This assigns a value to the variable james


'The value is the number 69
james = 69

'This assigns a value to the Excel cell E3


'The value is the value of the variable james
Cells(3, 5) = james

'This assigns a value to the variable james


'The value is the current value of the variable
'james + 1
james = james + 1

'This assigns a value to the Excel cell F3


'The value is the value of the variable james
Cells(3, 6) = james

'This assigns a value to the variable quinn


'The value is the text "PSB"
quinn = "PSB FML!"

'This assigns a value to the Excel cell E4


'The value is the value of the variable quinn
Cells(4, 5) = quinn

'This assigns a value to the variable quinn


'The value is the current value of the variable
'quinn CONCATENATED with " Rocks!"
quinn = quinn + " Rocks!"

'This assigns a value to the Excel cell F4


'The value is the value of the variable quinn
Cells(4, 6) = quinn

This is a loop
'It is controlled by variable rowNum
'It goes from 1 to 13
For rowNum = 1 To 13

'This assigns a value to the Excel cell


'row equal to the value of the variable
'rowNum, column C.
'The value is the text "FML!"
Cells(rowNum, 3) = "FML!"

'End loop rowNum


Next rowNum

'This is a loop
'It is controlled by variable rowNum
'It goes from 1 to 13
For rowNum = 3 To 13 Step 2

'This assigns a value to the Excel cell


'row equal to the value of the variable
'rowNum, column C.
'The value is the text "FML!"
Cells(rowNum, 3) = "FML! (" & rowNum & ")"

'End loop rowNum


Next rowNum

'This is a loop
'It is controlled by variable rowNum
'It goes from 4 to 13 in steps of 3
For rowNum = 4 To 13 Step 3

'This assigns a value to the Excel cell


'row equal to the value of the variable
'rowNum, column equal to the value of
'the variable rowNum.
'The value is the value of the variable
'rowNum.
Cells(rowNum, rowNum) = rowNum
Cells(rowNum, rowNum).Interior.ColorIndex = rowNum

'End loop rowNum


Next rowNum

MsgBox (rowNum)

'Start subroutine getValues


Sub getValues()

'Define variables
Dim iLower As Integer
Dim iUpper As Integer
Dim sLower As String
Dim sUpper As String

' (Get lower value from user)


'This assigns a value to the
'variable lower.
'The value is the return value from
'the Input Box.
getLower:
Do
sLower = InputBox("Please enter lower value ( 1 - 100 )")
Loop While IsNumeric(sLower) = False And sLower <> vbNullString
'IF Cancel THEN Exit Sub
If sLower = vbNullString Then
MsgBox ("Chicken!")
Exit Sub
ElseIf sLower < 1 Or sLower > 100 Then
MsgBox ("Idiot! Only 1 - 100 !!!")
GoTo getLower
End If

' (Get upper value from user)


'This assigns a value to the
'variable upper.
'The value is the return value from
'the Input Box.
getUpper:
Do
sUpper = InputBox("Please enter upper value ( " & sLower & " - 100 )")
Loop While IsNumeric(sUpper) = False And sUpper <> vbNullString

'IF Cancel THEN Exit Sub


If sUpper = vbNullString Then
Exit Sub
ElseIf sUpper < sLower Or sUpper > 100 Then
MsgBox ("Fool! Must be between " & sLower & " and 100")
GoTo getUpper
End If

'Transfer STRING version of variables to INTEGER version

'This assigns a value to the


'variable iLower.
'The value is the value of
'the variable sLower
iLower = sLower

'This assigns a value to the


'variable iUpper.
'The value is the value of
'the variable sUpper
iUpper = sUpper

'Call processPattern passing INTEGER variables


Call processPattern(iLower, iUpper)

'End subroutine
End Sub
'Start subroutine newGame
Sub newGame()

'Define LOCAL variables


Dim sUpperLimit As String

'Ensure TRUELY random


Randomize

'-----------------------------------------------
getUpperLimit:

'Ask user how big are their brains


Do
sUpperLimit = InputBox("How high can you go?")
Loop While IsNumeric(sUpperLimit) = False And sUpperLimit <> vbNullString

'IF cancel or X then message and quit


If sUpperLimit = vbNullString Then
MsgBox ("CHICKEN !!!!")
Exit Sub
'ELSEIF negative THEN error message and get new number
ElseIf sUpperLimit < 1 Then
MsgBox ("YOU ARE STUPID! Only positive numbers!")
GoTo getUpperLimit
End If
'-----------------------------------------------

'Generate random number 1 to


'value of upper limit variable
'and assign this to the
'variable iNumberToGuess
iNumberToGuess = Int(Rnd * sUpperLimit) + 1

'Clear and positon to Guess Cell (E3)


Cells(3, 5) = ""
Cells(3, 5).Select

'Display remaining guesses


Cells(1, 3) = "Remaining Guesses :"
Cells(1, 5) = Int(sUpperLimit / 5)

'Display value of variable iNumberToGuess


MsgBox (iNumberToGuess)
'End subroutine
End Sub

'Start subroutine checkGuess


Sub checkGuess()

'Define LOCAL variables


Dim iUserAttempt As Integer
Dim sUserAttempt As String

'Get user attempt STRING version


sUserAttempt = Cells(3, 5)

'IF blank THEN error message


If sUserAttempt = "" Then
MsgBox ("Please enter a value")

'ELSEIF non numeric THEN error message


ElseIf IsNumeric(sUserAttempt) = False Then
MsgBox ("IDIOT! Only numbers please!")

'ELSEIF outside range THEN error message


ElseIf sUserAttempt < 1 Or sUserAttempt > 20 Then
MsgBox ("STUPID! Between 1 and 20 only!")

'ELSEIF no remaining guesses THEN error message


ElseIf Cells(1, 5) < 1 Then
MsgBox ("No more guesses remain! You've lost!")

Else

'when we get here, no errors, so set up


'value of INTEGER version of user attempt

'This assigns a value to the


'variable iUserAttempt.
'The value is the value of the
'variable sUserAttempt
' (move STRING variable value to
' INTEGER variable value)
iUserAttempt = sUserAttempt

'IF correct THEN success message


If iUserAttempt = iNumberToGuess Then
MsgBox ("DAMN! You got it right!")
Else
'Guess is too high or too low
If iUserAttempt < iNumberToGuess Then
MsgBox ("Wrong! Ha Ha Ha! Try HIGHER!")
ElseIf iUserAttempt > iNumberToGuess Then
MsgBox ("Hee hee hee! Wrong! Try LOWER!")
End If

'Reduce remaining guesses by 1


Cells(1, 5) = Cells(1, 5) - 1

'If no more remaining guesses then message


If Cells(1, 5) < 1 Then
MsgBox ("Too Slow! You lost! LOSER!")
End If

End If

End If

'End subroutine
End Sub

Start subroutine newGame


Sub newGame()

'Define LOCAL variables


Dim sUpperLimit As String

'Ensure TRUELY random


Randomize

'-----------------------------------------------
getUpperLimit:

'Ask user how big are their brains


Do
sUpperLimit = InputBox("How high can you go?")
Loop While IsNumeric(sUpperLimit) = False And sUpperLimit <> vbNullString

'IF cancel or X then message and quit


If sUpperLimit = vbNullString Then
MsgBox ("CHICKEN !!!!")
Exit Sub
'ELSEIF negative THEN error message and get new number
ElseIf sUpperLimit < 1 Then
MsgBox ("YOU ARE STUPID! Only positive numbers!")
GoTo getUpperLimit
End If
'-----------------------------------------------

'Generate random number 1 to


'value of upper limit variable
'and assign this to the
'variable iNumberToGuess
iNumberToGuess = Int(Rnd * sUpperLimit) + 1

'Clear and positon to Guess Cell (E3)


Cells(3, 5) = ""
Cells(3, 5).Select

'Display remaining guesses


Cells(1, 3) = "Remaining Guesses :"
Cells(1, 5) = Int(sUpperLimit / 5)

'Display value of variable iNumberToGuess


MsgBox (iNumberToGuess)

'End subroutine
End Sub

'Start subroutine checkGuess


Sub checkGuess()

'Define LOCAL variables


Dim iUserAttempt As Integer
Dim sUserAttempt As String

'Get user attempt STRING version


sUserAttempt = Cells(3, 5)

'IF blank THEN error message


If sUserAttempt = "" Then
MsgBox ("Please enter a value")

'ELSEIF non numeric THEN error message


ElseIf IsNumeric(sUserAttempt) = False Then
MsgBox ("IDIOT! Only numbers please!")
'ELSEIF outside range THEN error message
ElseIf sUserAttempt < 1 Or sUserAttempt > 20 Then
MsgBox ("STUPID! Between 1 and 20 only!")

'ELSEIF no remaining guesses THEN error message


ElseIf Cells(1, 5) < 1 Then
MsgBox ("No more guesses remain! You've lost!")

Else

'when we get here, no errors, so set up


'value of INTEGER version of user attempt

'This assigns a value to the


'variable iUserAttempt.
'The value is the value of the
'variable sUserAttempt
' (move STRING variable value to
' INTEGER variable value)
iUserAttempt = sUserAttempt

'IF correct THEN success message


If iUserAttempt = iNumberToGuess Then
MsgBox ("DAMN! You got it right!")
Else
'Guess is too high or too low
If iUserAttempt < iNumberToGuess Then
MsgBox ("Wrong! Ha Ha Ha! Try HIGHER!")
ElseIf iUserAttempt > iNumberToGuess Then
MsgBox ("Hee hee hee! Wrong! Try LOWER!")
End If

'Reduce remaining guesses by 1


Cells(1, 5) = Cells(1, 5) - 1

'If no more remaining guesses then message


If Cells(1, 5) < 1 Then
MsgBox ("Too Slow! You lost! LOSER!")
End If

End If

End If

'End subroutine
End Sub
Start subroutine getValues
Sub getValues()

'Define variables
Dim iLower As Integer
Dim iUpper As Integer
Dim sLower As String
Dim sUpper As String

' (Get lower value from user)


'This assigns a value to the
'variable lower.
'The value is the return value from
'the Input Box.
getLower:
Do
sLower = InputBox("Please enter lower value ( 1 - 100 )")
Loop While IsNumeric(sLower) = False And sLower <> vbNullString

'IF Cancel THEN Exit Sub


If sLower = vbNullString Then
MsgBox ("Chicken!")
Exit Sub
ElseIf sLower < 1 Or sLower > 100 Then
MsgBox ("Idiot! Only 1 - 100 !!!")
GoTo getLower
End If

' (Get upper value from user)


'This assigns a value to the
'variable upper.
'The value is the return value from
'the Input Box.
getUpper:
Do
sUpper = InputBox("Please enter upper value ( " & sLower & " - 100 )")
Loop While IsNumeric(sUpper) = False And sUpper <> vbNullString

'IF Cancel THEN Exit Sub


If sUpper = vbNullString Then
Exit Sub
ElseIf sUpper < sLower Or sUpper > 100 Then
MsgBox ("Fool! Must be between " & sLower & " and 100")
GoTo getUpper
End If
'Transfer STRING version of variables to INTEGER version

'This assigns a value to the


'variable iLower.
'The value is the value of
'the variable sLower
iLower = sLower

'This assigns a value to the


'variable iUpper.
'The value is the value of
'the variable sUpper
iUpper = sUpper

'Call processPattern passing INTEGER variables


Call processPattern(iLower, iUpper)

'End subroutine
End Sub

You might also like