Conditional Formatting or Counting if within "x" of another value

mbpaul1987

New Member
Joined
Jan 15, 2014
Messages
15
I have a rather interesting situation that I can't seem to find an easy solution to. I have a few workbooks that have 3000+ lines of data in them. I need to find a way to pull a count of items that are within a certain distance of other items. As a very simplified example I will use the following table:
ItemLocation (mile)
I1
S3
B3.5
I4
I8
S9
I20
B21
I22
I24
S26

<tbody>
</tbody>

I need to find a way to to count the number of "I" that are within "2 miles" (this number will be variable) of an S. In the case of the example there are 4. My Initial thought was to try to find a way to conditionally format the "I" rows that meet that criteria then I can easily sort them and count them. However, Ultimately I am just looking for an overall count of items within a set distance from another Item.
ItemLocation (mile)
I1
S3
B3.5
I4
I8
S9
I20
B21
I22
I24
S26

<tbody>
</tbody>


Thanks for any Help

Michael
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe something like this


A
B
C
D
E
1
Item​
Location (mile)​
Distance​
Result​
2
I​
1​
2​
4​
3
S​
3​
4​
5​
4
B​
3,5​
5
I​
4​
6
I​
8​
7
S​
9​
8
I​
20​
9
B​
21​
10
I​
22​
11
I​
24​
12
S​
26​

Distances in D2, D3...

Array formula in E2 copied down
=SUM(IF(IF(A$2:A$12="I",IF(ISNUMBER(TRANSPOSE(IF(A$2:A$12="S",B$2:B$12))),ABS(B$2:B$12-TRANSPOSE(B$2:B$12))))<=D2,1))
confirmed with Ctrl+Shift+Enter, not just Enter

Hope this helps

M.
 
Upvote 0
A little simpler formula

E2 copied down
=SUM(IF(IF(A$2:A$12="I",ABS(B$2:B$12-TRANSPOSE(IF(A$2:A$12="S",B$2:B$12,9.99E+307))))<=D2,1))
Ctrl+Shift+Enter

M.
 
Upvote 0
Maybe something like this


A
B
C
D
E
1
Item​
Location (mile)​
Distance​
Result​
2
I​
1​
2​
4​
3
S​
3​
4​
5​
4
B​
3,5​
5
I​
4​
6
I​
8​
7
S​
9​
8
I​
20​
9
B​
21​
10
I​
22​
11
I​
24​
12
S​
26​

<tbody>
</tbody>


Distances in D2, D3...

Array formula in E2 copied down
=SUM(IF(IF(A$2:A$12="I",IF(ISNUMBER(TRANSPOSE(IF(A$2:A$12="S",B$2:B$12))),ABS(B$2:B$12-TRANSPOSE(B$2:B$12))))<=D2,1))
confirmed with Ctrl+Shift+Enter, not just Enter

Hope this helps

M.


Hi Marcelo,

Thanks for the reply! The solution works great for the small distances but now the issue that I'm having is when I get into longer distances that might overlap it may count the instance twice:
ItemLocation (mile)DistanceResult
I124
S345
B3.568
I4
I8
S9
I20
B21
I22
I24
S26

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
This is because some rows are being counted twice. For example, the distances from row5 (=4) to row3 (=3) and to row7(=9) are less or equal the value in D4(=6). If you do not want such double counting we need an alternative formula, that is:

E2 copied down
=SUM(--(MMULT(--(IF(A$2:A$12="I",ABS(B$2:B$12-TRANSPOSE(IF(A$2:A$12="S",B$2:B$12,9.99E+307))),9.99E+307)<=D2),ROW(A$2:A$12)^0)>0))
Ctrl+Shift+Enter


A
B
C
D
E
1
Item​
Location (mile)​
Distance​
Result​
2
I​
1​
2​
4​
3
S​
3​
4​
5​
4
B​
3,5​
6​
6​
5
I​
4​
6
I​
8​
7
S​
9​
8
I​
20​
9
B​
21​
10
I​
22​
11
I​
24​
12
S​
26​

<tbody>
</tbody>


M.
 
Last edited:
Upvote 0
This is because some rows are being counted twice. For example, the distances from row5 (=4) to row3 (=3) and to row7(=9) are less or equal the value in D4(=6). If you do not want such double counting we need an alternative formula, that is:

E2 copied down
=SUM(--(MMULT(--(IF(A$2:A$12="I",ABS(B$2:B$12-TRANSPOSE(IF(A$2:A$12="S",B$2:B$12,9.99E+307))),9.99E+307)<=D2),ROW(A$2:A$12)^0)>0))
Ctrl+Shift+Enter


A
B
C
D
E
1
Item​
Location (mile)​
Distance​
Result​
2
I​
1​
2​
4​
3
S​
3​
4​
5​
4
B​
3,5​
6​
6​
5
I​
4​
6
I​
8​
7
S​
9​
8
I​
20​
9
B​
21​
10
I​
22​
11
I​
24​
12
S​
26​

<tbody>
</tbody>


M.

Hi Marcelo,

That works perfectly. Thank you so much!! I must admit that array formulas are just not something that I have been able to fully wrap my head around and so to me they are about equivalent to magic ;). Once I'm done my current project I'm looking forward to doing a bit of research to properly understand how your formula works.

Again thanks so much!!

Michael
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,907
Members
449,478
Latest member
Davenil

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