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:

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,527
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:

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,527
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.
 

Forum statistics

Threads
1,082,359
Messages
5,364,916
Members
400,815
Latest member
Joaquin Phoenix

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top