Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Solutions - Conditional Statements – More Exercises
- -----------------------------------------------------------
- ' 01. Valid Triangle
- Sub ValidTriangle()
- Dim a As Integer, b As Integer, c As Integer
- a = Range("A1").Value
- b = Range("A2").Value
- c = Range("A3").Value
- If a < b + c And b < a + c And c < a + b Then
- Range("B1").Value = "Valid Triangle"
- Else
- Range("B1").Value = "Invalid Triangle"
- End If
- End Sub
- -----------------------------------------------------------
- ' 02. Coffee Shop
- Sub CoffeeShop()
- Dim drink As String, extra As String
- Dim price As Double
- drink = Range("A1").Value
- extra = Range("A2").Value
- If drink = "coffee" Then
- price = 1
- ElseIf drink = "tea" Then
- price = 0.6
- End If
- If extra = "sugar" Then
- price = price + 0.4
- End If
- Range("A3").Value = "Final price: $" & price
- End Sub
- -----------------------------------------------------------
- ' 03. Marketplace
- Sub Marketplace()
- Dim product As String, day As String
- Dim price As Double
- product = Range("A1").Value
- day = Range("A2").Value
- Select Case product
- Case "Kiwi"
- If day = "Weekday" Then
- price = 2.2
- Else
- price = 2.5
- End If
- Case "Banana"
- If day = "Weekend" Then
- price = 2.7
- Else
- price = 2.0
- End If
- Case "Apple"
- If day = "Weekend" Then
- price = 1.6
- Else
- price = 1.2
- End If
- End Select
- Range("A3").Value = price
- End Sub
- -----------------------------------------------------------
- ' 04. Bonus Score
- Sub BonusScore()
- Dim points As Integer
- points = Range("A1").Value
- Select Case points
- Case 0 To 3
- points = points + 5
- Case 4 To 6
- points = points + 15
- Case 7 To 9
- points = points + 20
- End Select
- Range("A2").Value = points
- End Sub
- -----------------------------------------------------------
- ' 05. Day Of Week
- Sub DayOfWeek()
- Dim dayNum As Integer
- dayNum = Range("A1").Value
- Select Case dayNum
- Case 1: Range("A2").Value = "Monday"
- Case 2: Range("A2").Value = "Tuesday"
- Case 3: Range("A2").Value = "Wednesday"
- Case 4: Range("A2").Value = "Thursday"
- Case 5: Range("A2").Value = "Friday"
- Case 6: Range("A2").Value = "Saturday"
- Case 7: Range("A2").Value = "Sunday"
- Case Else: Range("A2").Value = "Error"
- End Select
- End Sub
- -----------------------------------------------------------
- ' 06. Vowel or Consonant
- Sub VowelOrConsonant()
- Dim letter As String
- letter = Range("A1").Value
- If letter Like "[AEIOUaeiou]" Then
- Range("A2").Value = "Vowel"
- Else
- Range("A2").Value = "Consonant"
- End If
- End Sub
- -----------------------------------------------------------
- ' 07. Sorted Numbers
- Sub SortedNumbers()
- Dim num1 As Integer, num2 As Integer, num3 As Integer
- num1 = Range("A1").Value
- num2 = Range("A2").Value
- num3 = Range("A3").Value
- If num1 < num2 And num2 < num3 Then
- Range("B1").Value = "Ascending"
- ElseIf num1 > num2 And num2 > num3 Then
- Range("B1").Value = "Descending"
- Else
- Range("B1").Value = "Not sorted"
- End If
- End Sub
- -----------------------------------------------------------
- ' 08. Number Type
- Sub NumberType()
- Dim num As Integer
- num = Range("A1").Value
- If num > 0 Then
- Range("A2").Value = "positive"
- ElseIf num < 0 Then
- Range("A2").Value = "negative"
- Else
- Range("A2").Value = "zero"
- End If
- End Sub
- -----------------------------------------------------------
- ' 09. Product of Three Numbers
- Sub ProductOfThreeNumbers()
- Dim num1 As Double, num2 As Double, num3 As Double
- num1 = Range("A1").Value
- num2 = Range("A2").Value
- num3 = Range("A3").Value
- If num1 = 0 Or num2 = 0 Or num3 = 0 Then
- Range("B1").Value = "zero"
- ElseIf (num1 * num2 * num3) > 0 Then
- Range("B1").Value = "positive"
- Else
- Range("B1").Value = "negative"
- End If
- End Sub
- -----------------------------------------------------------
- ' 10. Area of Figures
- Sub AreaOfFigures()
- Dim figureType As String
- figureType = Range("A1").Value
- Select Case figureType
- Case "square"
- Dim side As Double
- side = Range("A2").Value
- Range("A4").Value = side * side
- Case "rectangle"
- Dim width As Double, length As Double
- width = Range("A2").Value
- length = Range("A3").Value
- Range("A4").Value = width * length
- Case "circle"
- Dim radius As Double
- radius = Range("A2").Value
- Range("A4").Value = WorksheetFunction.Pi() * radius * radius
- End Select
- End Sub
Advertisement
Comments
-
Comment was deleted
-
- This is correct format for result on rows 35 and 67
- With Range("A3")
- .Value = price
- .NumberFormat = "0.00"
- End With
-
- In 10. Area of Figures again missing formating in results:
- Sub Area_Of_Figures()
- Dim figureType As String
- figureType = Range("A1").Value
- Select Case figureType
- Case "square"
- Dim side As Double
- side = Range("A2").Value
- With Range("A4")
- .Value = side * side
- .NumberFormat = "0.00"
- End With
- Case "rectangle"
- Dim width As Double, length As Double
- width = Range("A2").Value
- length = Range("A3").Value
- With Range("A4")
- .Value = width * length
- .NumberFormat = "0.00"
- End With
- Case "circle"
- Dim radius As Double
- radius = Range("A2").Value
- Range("A4").Value = WorksheetFunction.Pi() * radius * radius
- With Range("A4")
- .Value = WorksheetFunction.Pi() * radius * radius
- .NumberFormat = "0.00"
- End With
- End Select
- End Sub
Add Comment
Please, Sign In to add comment
Advertisement