How to modify UDF into VBA Macro

Feroz90

Board Regular
Joined
Apr 25, 2019
Messages
52
Hi All,

The below UDF will make us to identify the positive and negative value if any value is split into multiple values.

Example, 100, -50, -50, -800, 200, 200, 200, 200
Code:
[TABLE]
<tbody>[TR]
[TD]Function Find_num(rng As Range, cell As Range, num_range As Single)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Dim com() As Single, c As Single, i As Single[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Dim s As Single, d As Single, u As Single, v As Single[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Dim arr() As String, r As Single, p As Single, t As Single[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Dim sum_cells As Single, j As Single, k As Single, l As Single[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]sum_cells = Application.WorksheetFunction.Sum(rng)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]c = rng.Rows.Count[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]ReDim arr(1 To 10000, 1 To c)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]r = 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]For i = 0 To Int(c / 2)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  If i <> Int(c / 2) - 1 Then[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    t = WorksheetFunction.Combin(c, i + 1)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  Else[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    t = WorksheetFunction.Combin(c, i + 1) / 2[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  End If[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  ReDim com(i)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  For j = 0 To i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    com(j) = j + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  Next[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  k = i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  For s = 1 To t[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    If com(k) > c And i > 0 Then[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      p = 0[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Do Until com(k) <= c - p[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        com(k - 1) = com(k - 1) + 1 [/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        k = k - 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        p = p + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Loop[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Do Until k >= i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        k = k + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        com(k) = com(k - 1) + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Loop[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    End If[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    d = 0[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    For j = 0 To i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      d = d + rng(com(j))[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    Next j[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    If d <= cell + num_range And d >= cell - num_range Then[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      For j = 1 To i + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        arr(r, j) = rng(com(j - 1))[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Next j[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      r = r + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    End If[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    If sum_cells - d <= cell + num_range And sum_cells - d >= cell - num_range Then[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      For j = 1 To c[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        v = 0[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        For u = 0 To i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]          If rng(com(u)) = rng(j) Then v = 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        Next u[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        If v = 0 Then[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]           arr(r, j) = rng(j)[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]        End If[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      Next j[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]      r = r + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    End If[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]    com(k) = com(k) + 1[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]  Next s[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Next i[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]Find_num = arr()[/TD]
[/TR]
</tbody>[/TABLE]
 
[TABLE]
<tbody>[TR]
[TD]End Function[/TD]
[/TR]
</tbody>[/TABLE]
Can it be used in a single column to highlight the matching values.
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I find it quite hard to understand how the function currently works. Can you give an example on how it would be used, and what the result then is?

And with example I mean:
what does the input look like
Is it an array formula
what is the output
 
Last edited:
Upvote 0
Ah, I understand.

The UDF function yourefer to looks in a list of values and highlights values that cancel eachother like 50 and -50. You are looking for something that would highlight 100 and 2 cells of -50 as the latter two will cancel the 100.

That will be a lot more work and definitely not doable with a formula. No doubt I could construct a macro to do this, but then for instance if all the numbers are in one list, the question is what needs to cancel what?

And also would the cancellation always be one way (100 and 2x -50, 800 and 4x-200, but not -100 and 2x 50) or would it be both ways? The latter i think is far too complicated, too many permutations. And are there any other things that can help decide on whether the cancellation is ok, like dates close to each other, or names of companies, etc.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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