Code does not always return the right answer.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Good afternoon, everybody, I hope you got a good weekend.

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
ABCDE
1SCALEPERS
2122
325
431
545
653
7610
8710
983
1096
111015
12111
131211
141310
151411
161511
17167
18172
19182
20197
212023
22217
23222
24231
25243
26251
27262
28278
29289
302911
313010
323120
33328
34338
353417
36355
373612
383713
39389
40395
414013
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
 

bebo021999

Hello, and thank you for your input.
Do not know actually VBA is needed
I really believe is better with VBA ("if you don't mind, of course") because it avoids the word "OUT" would be better to use the last row count option.
Then all the numbers will be founded.
then I have the bin of 36 numbers full.
And I am so glad you got a perfect understanding of my question, mean your formula work, and again thank you, Sir.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,216,487
Messages
6,130,943
Members
449,608
Latest member
jacobmudombe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top