Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Exercise18MacroOne()
- '
- ' Exercise18MacroOne Macro
- '
- 'set the random number generator seed
- Randomize
- Dim MyValue As Single
- MyValue = Rnd
- 'to get a range we'll have to do some weird things
- 'Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
- With Worksheets("Exercise 18")
- Range("E1").Value = MyValue
- End With
- 'now, let's create an array and write it to some cells
- 'in the currently active worksheet
- Dim Fruits(1 To 8) As String
- Fruits(1) = "Bananas"
- Fruits(2) = "Grapes"
- Fruits(3) = "Oranges"
- Fruits(4) = "Apples"
- Fruits(5) = "Peaches"
- Fruits(6) = "Watermelon"
- Fruits(7) = "Papaya"
- Fruits(8) = "Durian"
- 'For i = 1 To 8
- ' Cells(i, 3) = Fruits(i)
- 'Next i
- 'Origin array
- Dim Origin(1 To 2) As String
- Origin(1) = "Imported"
- Origin(2) = "Local"
- 'Days array
- Dim Days(1 To 7) As String
- Days(1) = "Monday"
- Days(2) = "Tuesday"
- Days(3) = "Wednesday"
- Days(4) = "Thursday"
- Days(5) = "Friday"
- Days(6) = "Saturday"
- Days(7) = "Sunday"
- Dim FruitIndex As Integer
- Dim OriginIndex As Integer
- Dim DaysIndex As Integer
- For i = 1 To 1000
- Randomize
- FruitIndex = Int((8 - 1 + 1) * Rnd + 1)
- OriginIndex = Int((2 - 1 + 1) * Rnd + 1)
- 'if the fruit is Papaya or Durian, then do not allow index 2 (local)
- If FruitIndex = 7 Or FruitIndex = 8 Then
- OriginIndex = 1
- End If
- DaysIndex = Int((7 - 1 + 1) * Rnd + 1)
- 'now write to the worksheet
- With Worksheets("Exercise 18")
- 'Fruits
- .Cells(i, 1) = Fruits(FruitIndex)
- 'Origin
- .Cells(i, 2) = Origin(OriginIndex)
- 'Days
- .Cells(i, 3) = Days(DaysIndex)
- End With
- Next i
- End Sub
- Sub Exercise18HideDurian()
- 'used to hide rows based on whether we find Durian in the data
- Dim BeginRow As Integer
- Dim EndRow As Integer
- Dim CheckColumn As Integer
- BeginRow = 1
- EndRow = 1000
- CheckColumn = 1
- 'go through and look for Durian
- For RowCount = BeginRow To EndRow
- With Worksheets("Exercise 18")
- If Cells(RowCount, CheckColumn).Value = "Durian" Then
- Cells(RowCount, CheckColumn).EntireRow.Hidden = True
- End If
- End With
- Next RowCount
- End Sub
- Sub Exercise18ShowDurian()
- 'used to show rows based on whether we find Durian in the data
- Dim BeginRow As Integer
- Dim EndRow As Integer
- Dim CheckColumn As Integer
- BeginRow = 1
- EndRow = 1000
- CheckColumn = 1
- 'go through and look for Durian
- For RowCount = BeginRow To EndRow
- With Worksheets("Exercise 18")
- If Cells(RowCount, CheckColumn).Value = "Durian" Then
- Cells(RowCount, CheckColumn).EntireRow.Hidden = False
- End If
- End With
- Next RowCount
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment