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
Can it be used in a single column to highlight the matching values.
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]
Last edited by a moderator: