Professional Documents
Culture Documents
Anotações Programação p2
Anotações Programação p2
Sub myFirstSub()
End Sub
Sub mySecondSub()
End Sub
Sub myThirdSub()
End Sub
'This is a loop
'It is controlled by variable rowNum
'It goes from 1 to 13
For rowNum = 1 To 13
End Sub
Sub loop2()
'This is a loop
'It is controlled by variable rowNum
'It goes from 1 to 13
For rowNum = 3 To 13 Step 2
End Sub
Sub loop3()
Dim rowNum As Integer
'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
MsgBox (rowNum)
End Sub
Sub makePattern()
For rowNum = 2 To 11
For colNum = 5 To 14
Next colNum
Next rowNum
End Sub
For colNum = 5 To 14
For rowNum = 2 To 11
Next rowNum
Next colNum
End Sub
'Define variables
Dim iLower As Integer
Dim iUpper As Integer
Dim sLower As String
Dim sUpper As String
'End subroutine
End Sub
'This makes Excel a *itch
'This ensures all variables are defined
'as VBA is now STRONGLY TYPED
' (This is done with a DIM statement)
Option Explicit
'-----------------------------------------------
getUpperLimit:
'End subroutine
End Sub
Else
End If
End If
'End subroutine
End Sub
Sub sub1()
james = 69
MsgBox (james)
Call sub2
MsgBox (james)
MsgBox (iNumberToGuess)
End Sub
Sub sub2()
End Sub
'End subroutine
End Sub
Else
End If
End If
'End subroutine
End Sub
Sub sub1()
iNumberToGuess = 1969
james = 69
MsgBox (james)
Call sub2
MsgBox (james)
MsgBox (iNumberToGuess)
End Sub
Sub sub2()
End Sub
'ColorIndex values:
' 0 = none
' 1 = black
' 2 = white
' 3 = red
' 4 = green
' 5 = blue
' 6 = yellow
Sub yellowLoop()
colNum = 3
For rowNum = 2 To 5
'-----------------------------
'Set back to yellow
'-----------------------------
'This assigns a value to the
'ColorIndex property of the
'Interior property of the
'Excel cell at row equal to
'the value of variable rowNul,
'column equal to the value
'of variable colNum.
'The value is the number 6
Cells(rowNum, colNum).Interior.ColorIndex = 6
If rowNum = 2 Then
Cells(rowNum, colNum) = "Andy"
ElseIf rowNum = 3 Then
Cells(rowNum, colNum) = "Bill"
ElseIf rowNum = 4 Then
Cells(rowNum, colNum) = "Chloe"
ElseIf rowNum = 5 Then
Cells(rowNum, colNum) = "David"
End If
colNum = colNum + 3
Next rowNum
End Sub
Sub redLoop()
'Initialise colNum
colNum = 13
For rowNum = 12 To 15
Cells(rowNum, colNum).Interior.ColorIndex = 3
colNum = colNum - 1
Next rowNum
End Sub
Sub blueLoop()
james = 3
Next colNum
Next rowNum
End Sub
Sub blueLoopNEW()
james = 3
Next rowNum
Next colNum
End Sub
Option Explicit
Sub yellowLoop()
'This is a loop
'It uses the variable yellow to control the loop
'It will have the values 2, 3, 4 and 5
For yellow = 2 To 5
Cells(yellow + 1, yellow).Interior.ColorIndex = 6
Next yellow
End Sub
Sub yellowLoop2()
'This is a loop
'It uses the variable yellow to control the loop
'It will have the values 2, 3, 4 and 5
For yellow = 2 To 5
Cells(yellow + 1, yellow).Interior.ColorIndex = 6
Next yellow
End Sub
Sub redLoop()
colNumber = 7
For red = 3 To 9 Step 2
Cells(red, colNumber).Interior.ColorIndex = 3
colNumber = colNumber + 1
Next red
End Sub
Sub redLoop2()
For red = 7 To 10
Cells(rowNumber, red).Interior.ColorIndex = 3
rowNumber = rowNumber + 2
Next red
End Sub
Sub blueLoop()
rowNumb = 12
End Sub
Sub blueLoop3()
rowNumb = 12
Sub blueLoop2()
colNumb = 8
For rowNumb = 10 To 12
Cells(rowNumb, colNumb).Interior.ColorIndex = 5
colNumb = colNumb - 3
Next rowNumb
End Sub
Sub blackLoop()
colNumb = 2
difference = 1
For rowNumb = 15 To 19
Cells(rowNumb, colNumb).Interior.ColorIndex = 1
colNumb = colNumb + difference
difference = difference + 1
Next rowNumb
End Sub
Sub greenLoop()
colNumb = 2
difference = 2
For rowNumb = 21 To 28
Cells(rowNumb, colNumb).Interior.ColorIndex = 4
colNumb = colNumb + difference
difference = difference * -1
Next rowNumb
End Sub
Sub orangeLoop()
colNumb = 6
difference = 1
For rowNumb = 20 To 28
Cells(rowNumb, colNumb).Interior.ColorIndex = 44
colNumb = colNumb + difference
If colNumb = 8 Then
difference = -1
ElseIf colNumb = 6 Then
difference = 1
End If
Next rowNumb
End Sub
'Start of Loop
Do
age = InputBox("Please enter your age")
'End loop
'Will loop while value of variable age
'not numeric and not cancel
Loop While IsNumeric(age) = False And age <> vbNullString
'Start of IF statement
If age = vbNullString Then
MsgBox ("Loser!")
Exit Sub
ElseIf age > 30 Then
MsgBox ("Too Old!!!")
ElseIf age < 18 Then
MsgBox ("Too Young!!!")
Else
MsgBox ("Perfect!")
'End of IF statement
End If
'End subroutine
End Sub
'Define variables
Dim james As Integer
Dim fontColor As Integer
Dim backColor As Integer
'End siubroutine
End Sub
Sub sillyLoops()
'--------------------------------------------------
' Loop a)
'--------------------------------------------------
Content = 4
Next colNum
Next rowNum
'--------------------------------------------------
' Loop b)
'--------------------------------------------------
colNum = 3
For rowNum = 15 To 19
Cells(rowNum, colNum).Interior.ColorIndex = 3
If rowNum = 15 Then
Content = "All"
ElseIf rowNum = 16 Then
Content = "Boys"
ElseIf rowNum = 17 Then
Content = "Cry"
ElseIf rowNum = 18 Then
Content = "Deeply"
Else
Content = "Everyday"
End If
colNum = colNum + 1
Next rowNum
'--------------------------------------------------
' Loop c)
'--------------------------------------------------
colNum = 11
Cells(rowNum, colNum).Interior.ColorIndex = 4
colNum = colNum + 1
Next rowNum
'--------------------------------------------------
' Loop d)
'--------------------------------------------------
colNum = 2
diff = 2
For rowNum = 21 To 24
Cells(rowNum, colNum).Interior.ColorIndex = 1
Cells(rowNum, colNum).Font.ColorIndex = 2
Cells(rowNum, colNum) = diff / 2
diff = diff * 2
Next rowNum
End Sub
Option Explicit
'Define GLOBAL variables
Dim wordToGuess As String
Dim userAttempt As String
Dim x As Integer
Dim bodyPartsLeft As Integer
'Merge J2 to J8
Range("J2:J8").Font.Name = "Courier New"
Range("J2:J8").Font.Bold = True
Range("J2:J8").Font.Size = 11
Range("J2:J8").Interior.ColorIndex = 37
Range("J2:J8") = ""
'End of subroutine
End Sub
'---------------------------------------------------
'Validate User Input:
'---------------------------------------------------
'IF guessed the word THEN error
If InStr(1, userAttempt, "-") = 0 Then
Cells(11, 10) = "BIRD BRAIN! You've already Won!!!"
'---------------------------------------------------
'ELSE IF GET HERE, THEN LEGO MOVIE (LIFE IS AWESOME!)
'---------------------------------------------------
Else
'IF letter does not exist in word THEN message and add to wrong list
If x = 0 Then
Cells(11, 10) = "Ha Ha! Wrong! The noose is tightening!"
Cells(15, 8) = Cells(15, 8) & " " & letterGuess
End If
End If
'End subroutine
End Sub
Sub success()
End Sub
Sub oneStepCloserToDeath()
If bodyPartsLeft = 9 Then
Cells(8, 10) = "'========="
ElseIf bodyPartsLeft = 8 Then
Cells(3, 10) = "' |"
Cells(4, 10) = "' |"
Cells(5, 10) = "' |"
Cells(6, 10) = "' |"
Cells(7, 10) = "' |"
ElseIf bodyPartsLeft = 7 Then
Cells(2, 10) = "' ======="
ElseIf bodyPartsLeft = 6 Then
Cells(3, 10) = "' \ |"
Cells(4, 10) = "' \|"
ElseIf bodyPartsLeft = 5 Then
Cells(2, 10) = "' .======="
Cells(3, 10) = "' : \ |"
ElseIf bodyPartsLeft = 4 Then
Cells(4, 10) = "' o |"
Cells(5, 10) = "' | |"
ElseIf bodyPartsLeft = 3 Then
Cells(6, 10) = "'/ |"
ElseIf bodyPartsLeft = 2 Then
Cells(6, 10) = "'/ \ |"
ElseIf bodyPartsLeft = 1 Then
Cells(5, 10) = "'-| |"
ElseIf bodyPartsLeft = 0 Then
Cells(5, 10) = "'-|- |"
End If
End Sub
Option Explicit
Sub getJames()
getNumberOfLetters:
Do
stNumberLetters = InputBox("How many letters (3-10)")
Loop While IsNumeric(stNumberLetters) = False And stNumberLetters <> vbNullString
End Sub
stCode = "!;"
iRand = Int(Rnd * 3) + 1
If iRand = 1 Then
stCode = stCode & ":"
ElseIf iRand = 2 Then
stCode = stCode & "/"
ElseIf iRand = 3 Then
stCode = stCode & "!"
End If
Next iCount
End Sub
Sub jamesLoops()
'-----------------------------------------------
' Loop a)
'-----------------------------------------------
contents = 13
contents = contents + 1
Next colNum
Next rowNum
'-----------------------------------------------
' Loop b)
'-----------------------------------------------
colNum = 3
For rowNum = 15 To 19
Cells(rowNum, colNum).Interior.ColorIndex = 4
colNum = colNum + 1
Next rowNum
'-----------------------------------------------
' Loop c)
'-----------------------------------------------
colNum = 15
For rowNum = 15 To 19
Cells(rowNum, colNum).Interior.ColorIndex = 3
If rowNum = 15 Then
word = "Everyday"
ElseIf rowNum = 16 Then
word = "Deeply"
ElseIf rowNum = 17 Then
word = "Cry"
ElseIf rowNum = 18 Then
word = "Boys"
ElseIf rowNum = 19 Then
word = "All"
End If
colNum = colNum - 1
Next rowNum
'-----------------------------------------------
' Loop d)
'-----------------------------------------------
colNum = 2
contents = 16
For rowNum = 21 To 25
Cells(rowNum, colNum).Interior.ColorIndex = 1
Cells(rowNum, colNum) = contents
contents = contents / 2
colNum = colNum + contents
Next rowNum
End Sub