I need help with a macro please

mrc44

Board Regular
Joined
Aug 12, 2017
Messages
64
I have a data table (B:E) with values that looks something like this:

IDDATA
485710
4743
466112130
45
4418
431235
42256
411213
etc

<tbody>
</tbody>

So the data table may have a maximum of 4 values. Some rows may have no data at all or less than 4 values as can be seen in the sample data
above. I need to calculate and get a count of variations/ deviations (within the range of -1,0,+1) among the rows. (-1/+1 if there is 1 one difference between two values of compared rows, and 0 if the values are the same).

So as an example, if you compare the first two rows above, 4 and 5 are in +1/-1 range. the others are not in range. so count is 1 for this comparison. if we compare the first and third rows, 5 and 6, 6 and 7, 10 and 11 are in +1/-1 range again. so count is 3.
Expected results would look like this:


EXPECTED RESULTS
ID4746454443424140etc
481301120
47000
46
45
44
43

<tbody>
</tbody>

Can I get help to achieve this result please? The results don't have to be on the same page. thank you!
This is a sample file :
https://1drv.ms/x/s!AsEuzEWSPBGqeyC5dokD4mMgr3w
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi mrc,
after messing around with formulas and array formulas I arrived at a very long formula that gave me the results, but I get why you want a macro, as that's much easier. Did you already generate some code? If not, this function should do the trick:

Code:
Function DiffMax(Rng1 As Range, Rng2 As Range, Optional MaxDiff As Double = 1) As Long

Res = 0
If Rng1.Cells.Count >= 1 And Rng2.Cells.Count >= 1 Then
    For Each c1 In Rng1.Cells
        If Trim(c1.Value) <> "" Then
            For Each c2 In Rng2.Cells
                If Trim(c2.Value) <> "" Then
                    If Abs(c1.Value - c2.Value) <= MaxDiff Then
                        Res = Res + 1
                    End If
                End If
            Next c2
        End If
    Next c1
End If
DiffMax = Res

End Function

After that, this is the formula in your example sheet, first cell in your results table, you can drag it to the other cells.
=DiffMax($B3:$E3,OFFSET($B$4:$E$4,COLUMN()-8,0))


Cheers,

Koen
 
Last edited:
Upvote 0
Thanks, Koen! I tried your code, it generates the results as expected. Since it is an array of values, and there are null cells too, I thought it'd be too problematic, and may not doable at all. And I wouldn't be able to write the vba code for this function. thanks for your help and time!
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,803
Members
449,261
Latest member
Rachel812321

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