abberyfarm
Well-known Member
- Joined
- Aug 14, 2011
- Messages
- 733
Hi there,
Would anybody be able to help me make a slight modification to the code below.
This code divides up the total distance travelled into individual journey distances based on the number of journeys.
I need to prevent it from generating 0 as a random number as 0 distance would not make sense. I believe I need to change the conditions for the array tbl, but I am not entirely sure.
Thank for help
John
Would anybody be able to help me make a slight modification to the code below.
Code:
[LEFT]Sub RandomQuicker()
Dim a&, b&, c&, i&, j&, tbl()
Randomize
For i = 2 To 10
If Cells(i, 1).Value = 1 Then
Cells(i, 3).Value = Cells(i, 2).Value
Else
If Cells(i, 1).Value = 2 Then
Cells(i, 3).Value = Int(1 + Rnd * Cells(i, 2).Value)
Cells(i, 4).Value = Cells(i, 2).Value - Cells(i, 3).Value
Else
a = Cells(i, 1).Value
b = Cells(i, 2).Value
ReDim Preserve tbl(1 To a - 1)
For j = 1 To a - 1
tbl(j) = Int((1 + b * Rnd))
Next j
c = Application.Sum(tbl)
For j = 1 To a - 1
tbl(j) = Int(tbl(j) / (1.1 * c) * b)
Next j
c = Application.Sum(tbl)
ReDim Preserve tbl(1 To a)
tbl(a) = b - c
Cells(i, 3).Resize(, UBound(tbl)) = tbl
End If
End If
Next i
End Sub
[/LEFT]
This code divides up the total distance travelled into individual journey distances based on the number of journeys.
Excel Workbook | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | No .Trips | Total travel | Individual travel | |||||
2 | 2 | 10 | 5 | 15 | ||||
3 | 3 | 20 | 3 | 5 | 12 | |||
4 | 4 | 40 | 5 | 10 | 12 | 13 | ||
Sheet1 |
I need to prevent it from generating 0 as a random number as 0 distance would not make sense. I believe I need to change the conditions for the array tbl, but I am not entirely sure.
Thank for help
John
Last edited: