montecarlo2012
Well-known Member
- Joined
- Jan 26, 2011
- Messages
- 984
- Office Version
- 2010
- Platform
- Windows
Good afternoon, everybody, I hope you got a good weekend.
This code counts how many rows each number from 1 to 36 is separated from line one.
My problem here is, the code is working, but, gives me the wrong results on certain numbers
and the right results on others.
I tried on many ways, tested many ways but still, right and wrong results.
Please, I need your help.
An example of wrong is when I plug number 2, in this scenario the right result would be "0", and the code return 2.
An example of a right is when I plug the number 13, in this case, the right result would be 36, and the code returns 36
Comments on the code are more than welcome.
thank you
VBA Code:
Sub Distance_one()
Set rngData = Range("B2", "B40")
For Each cell In rngData
If cell = 5 Then
Range("E2").Offset(0, m) = n
n = -1
Else
n = n + 1
End If
Next
End Sub
This code counts how many rows each number from 1 to 36 is separated from line one.
My problem here is, the code is working, but, gives me the wrong results on certain numbers
and the right results on others.
I tried on many ways, tested many ways but still, right and wrong results.
Please, I need your help.
Book1 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | SCALE | PERS | |||||
2 | 1 | 2 | 2 | ||||
3 | 2 | 5 | |||||
4 | 3 | 1 | |||||
5 | 4 | 5 | |||||
6 | 5 | 3 | |||||
7 | 6 | 10 | |||||
8 | 7 | 10 | |||||
9 | 8 | 3 | |||||
10 | 9 | 6 | |||||
11 | 10 | 15 | |||||
12 | 11 | 1 | |||||
13 | 12 | 11 | |||||
14 | 13 | 10 | |||||
15 | 14 | 11 | |||||
16 | 15 | 11 | |||||
17 | 16 | 7 | |||||
18 | 17 | 2 | |||||
19 | 18 | 2 | |||||
20 | 19 | 7 | |||||
21 | 20 | 23 | |||||
22 | 21 | 7 | |||||
23 | 22 | 2 | |||||
24 | 23 | 1 | |||||
25 | 24 | 3 | |||||
26 | 25 | 1 | |||||
27 | 26 | 2 | |||||
28 | 27 | 8 | |||||
29 | 28 | 9 | |||||
30 | 29 | 11 | |||||
31 | 30 | 10 | |||||
32 | 31 | 20 | |||||
33 | 32 | 8 | |||||
34 | 33 | 8 | |||||
35 | 34 | 17 | |||||
36 | 35 | 5 | |||||
37 | 36 | 12 | |||||
38 | 37 | 13 | |||||
39 | 38 | 9 | |||||
40 | 39 | 5 | |||||
41 | 40 | 13 | |||||
Sheet1 |
An example of wrong is when I plug number 2, in this scenario the right result would be "0", and the code return 2.
An example of a right is when I plug the number 13, in this case, the right result would be 36, and the code returns 36
Comments on the code are more than welcome.
thank you