Change short macro to a function (UDF)

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
771
Office Version
  1. 365
Platform
  1. Windows
Sample cell contents

G5 contains 10
G6 contains 20
G7 contains 30
G8 contains 40

AA15 contains G5, G6, G7, G8

AB15 contains 10, 15, 20, 35, 40

I've been using the following macro to compare the numbers in cell AB15 to those in cells G5, G6, G7, and G8 --- and to return any where there is not a match into cell AC15. In this example, the result in AC15 would be 15, 35.

Code:
Sub FindBadNums()
Dim A, S, K, T&, Ta&, Op$

S = Split(Range("AA15"), ", ")
A = Split(Range("AB15"), ", ")

For T = 0 To UBound(A)
K = A(T) + 0
    For Ta = 0 To UBound(S)
    If Range(Trim(S(Ta))) = K Then K = "": Exit For
    Next Ta
If K <> "" Then Op = Op & ", " & K
Next T

If Op <> "" Then Range("AC15") = Mid(Op, 2)
End Sub

Is there a way to convert this to a function that could be entered into cell AC15, such as something like =FindBadNums(AB15)?

Thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
@ChristineJ Give this a try.
Copy the code to a Code Module

VBA Code:
Function FindBadNums(GoodNums, CheckNums) As String
Dim A, S, K, T&, Ta&, Op$
S = Split(GoodNums, ", ")
A = Split(CheckNums, ", ")

For T = 0 To UBound(A)
K = A(T) + 0
    For Ta = 0 To UBound(S)
    If Range(Trim(S(Ta))) = K Then K = "": Exit For
    Next Ta
If K <> "" Then Op = Op & ", " & K
Next T
If Op <> "" Then FindBadNums = Mid(Op, 2)

End Function

Book1
GHIJKLMNOPQRSTUVWXYZAAABAC
510
620
730
840
9
10
11
12
13
14
15G5, G6, G7, G810, 15, 20, 35, 40 15, 35
16
Sheet6
Cell Formulas
RangeFormula
AC15AC15=FindBadNums(AA15,AB15)

HTH
 
Upvote 0
Solution
I think you can just make it a public function and then use "FindBadNums" in a formula.

VBA Code:
Public Function FindBadNums(rng, nums) As String

Dim A, S, K, T&, Ta&, Op$

S = Split(rng, ", ")
A = Split(nums, ", ")

For T = 0 To UBound(A)
K = A(T) + 0
    For Ta = 0 To UBound(S)
    If Range(Trim(S(Ta))) = K Then K = "": Exit For
    Next Ta
If K <> "" Then Op = Op & ", " & K
Next T


If Op <> "" Then FindBadNums = Mid(Op, 2)

End Function
 
Upvote 0
With Excel 365, try a formula like the following

T202405.xlsm
ABCDE
1
210, 15, 20, 35, 40
3
4
510
620
730
840
9
101535
2h
Cell Formulas
RangeFormula
C10:D10C10=LET(a,--TEXTSPLIT(D2,","),FILTER(a, NOT(COUNTIF(B5:B8, a))))
Dynamic array formulas.
 
Upvote 0
With Excel 365, try the following. I do not know if you want the results in one cell or separate cells; consequently several different versions.


T202405.xlsm
ABCDEFG
1
210, 15, 20, 35, 40
315, 3515, 35
415351535
510
620
730
840
9
2h
Cell Formulas
RangeFormula
D3D3=LET(r_1,D2,r_2,B5:B8,a,--TEXTSPLIT(r_1,","),ARRAYTOTEXT(FILTER(a, NOT(COUNTIF(r_2, a)))))
D4:E4D4=LET(r_1,D2,r_2,B5:B8,a,--TEXTSPLIT(r_1,","),FILTER(a, NOT(COUNTIF(r_2, a))))
F3F3=MissingNums(D2,B5:B8)
F4:G4F4=Distinct_a(D2,B5:B8)
Dynamic array formulas.
Lambda Functions
NameFormula
Distinct_a=LAMBDA(r_1,r_2, LET(a, --TEXTSPLIT(r_1, ","), FILTER(a, NOT(COUNTIF(r_2, a))) ) )
MissingNums=LAMBDA(r_1,r_2,LET(a, --TEXTSPLIT(r_1, ","),ARRAYTOTEXT(FILTER(a, NOT(COUNTIF(r_2, a)))) ))
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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