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
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
What are m and n; undeclared variables (if they are, then their data type is variant)?
It's always my practice to use Option Explicit.
Nor have you declared rngData (or at least showed that) and I'd expect Excel to think that Cell is a variable as well.

Aside from that, I can't make sense of this
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
 
Upvote 0
OK, I think I get it now. But for 2 the correct answer would be 26? For 13 it would be 37? Your code only enters a value one time in E2 yet there are often several instances of the number being searched. So is this supposed to put a value from A in each row where found? Probably doesn't because m is always zero.

how many rows each number from 1 to 36 is separated from line one.
You said the right answer for 2 would be 0 but 2 is 1 line removed from line 1?
 
Upvote 0
I was considering the "first" row to be row #1 which contains your headers. Ignoring that, then maybe this is what you need:
VBA Code:
Sub Distance_one()
Dim rngData As Range, cel As Range
Dim m As Integer, n As Integer

Set rngData = Range("B2", "B40")
Application.EnableEvents = False
For Each cel In rngData
   If cel = 5 Then '<<why not use a cell value or input box for the value instead of modifying code for each value tested??
      Range("E2").Offset(m, 0) = m
   Else
       n = n + 1
   End If
   m = m + 1
Next
Application.EnableEvents = True
End Sub
 
Upvote 0

Micron

Thank you so much for your code, beside you made me understand about how really the counting must be, and you are completely right
also, you have a way better knowledge of programming than me.
Please let me show you now with your input what kind of results I am really expecting.
mr excel question.xlsm
ABCDEF
1SCALEPERSBINLOCATION
21213
32521
43135
5454out
65352
761069
8710719
983827
1096938
111015106
121111112
1312111236
1413101337
15141114out
1615111510
1716716OUT
181721734
1918218
2019719
21202320
2221721
2322222
2423123
2524324
2625125
2726226
2827827
2928928
30291129
31301030
32312031
3332832
3433833
35341734
3635535
37361236
383713
39389
40395
414013
Sheet1

Please help me here, I got this code really fishing on the internet.
Thank you Micron.
 
Upvote 0
It seems like the goal posts have moved. If that continues to happen I can't help you. I thought I understood at one point but have to admit I see no logic to what you posted.
 
Upvote 0
Sorry, my interpretation looks like that. As soon as I understood your statement about //
You said the right answer for 2 would be 0 but 2 is 1 line removed from line 1?
At this point when looks like a move to the goal, I just adjusted to this idea that is better than my idea in the first place.
So is this supposed to put a value from A in each row where found?
Here is when I thought you are again right, so from here put the Bin of the numbers that needed to be searched for the location
Really looks like the goal and question change but, I am not sure if that affirmation is entirely true
I am a boat navigating with your wind.
Anyway if you decide the opposite I really understand your position.
I really appreciate your kindness, thank you very much for your time, and sorry if I bother you.
 
Upvote 0
So for the Bins 18, 19, 21, 22 Location value is "out" and for Bin 23 Location value is 20?

What are the Location values for Bin 24 to 36, because your Pers values don't go beyond 23?

And why does Bin 2 = Location 1 when there are at least 5 Pers rows that are 2? Only the first instance of Pers 2 is considered?

If none of that makes sense then I have no idea what you want now. Even if it does make sense I'm not sure if I can help.
 
Upvote 0
Sorry again, Micron.
not sure if I can help.
I understand.
Just in case, a curious thing happens and some help happens I am adding some explanation, thanks.
PERS. is an array with 9000 rows, I pick 40 for illustration purposes, the reason why for this post I rather use the term "out", assuming when I use the code with the real data then all the rows will be filling out
24 to 36, because your Pers values don't go beyond 23?
the same reason, this is partial info.
The original code finds the distance among the same value in the 9k rows, but here and now the only thing I need is to count from the beginning of the array until a number is found for the
first time only, and only for the first time, no more than that, one time. That's the answer to this question
why does Bin 2 = Location 1 when there are at least 5 Pers rows that are 2?
Thanks. I really appreciate your concern.
 
Upvote 0
Do not know actually VBA is needed or not, but lets start with formula first:

Book1
ABCDEFG
1SCALEPERSBINLOCATIONYour expectation
212133
325211
431355
5454outout
653522
7610699
871071619
98382727
109692838
1110151066
12111111212
131211123636
141310133737
15141114outout
161511151010
1716716outOUT
18172173434
1918218out
2019719out
2120232031
2221721out
2322222out
242312320
2524324out
2625125out
2726226out
2827827out
2928928out
30291129out
31301030out
32312031out
3332832out
3433833out
35341734out
3635535out
37361236out
383713
39389
40395
414013
Sheet1
Cell Formulas
RangeFormula
E2:E37E2=IFERROR(MATCH(D2,$B$2:$B$41,0),"out")
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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