How to create a UDF from a formula

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
123
HI,

I have a quite large formula i use on a regular basis to reformat some data that comes out of our sql. i would like to create a udf from it in order to shorten the formula.

The formula is as follows:

=IF(SUMPRODUCT(LEN(A2)-LEN(SUBSTITUTE(A2,{1,2,3,4,5,6,7,8,9,0},"")))=0,"Invalid Postcode",IF(SUMPRODUCT(LEN(A2)-LEN(SUBSTITUTE(A2,{1,2,3,4,5,6,7,8,9,0},"")))>3,"invalid postcode",IF(LEN(SUBSTITUTE(A2," ",""))<5,"Invalid Postcode",IF(LEN(SUBSTITUTE(A2," ",""))>8,"Invalid Postcode",TRIM(CONCATENATE(UPPER(LEFT(SUBSTITUTE(A2," ",""),LEN(SUBSTITUTE(A2," ",""))-3))," ",UPPER(RIGHT(SUBSTITUTE(A2," ",""),3))))))))

I'm not a complete novice with VBA, but would just like a little guidance on how to go about creating UDF's.

Thanks,

Dan.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This doesn't solve your problem but your post has highlighted a possible error in your formula.

The first two conditions of your formula are counting the number of digits 0-9 that exist in A2.
If this is 0 or greater than 3 then the postcode is invalid.

The third condition is removing all spaces from A2 and is the length of this is < 5 or > 8 then again the postcode is invalid.

However, here's a legitimate postcode "W1A 4WW" If that gets typed as "+1A WW" or "W1A 4$£" your formula doesn't give an invalid password error!!

There's probably a UDF already written for checking legitimate postcodes that checks alphabetic characters as well.

Thinking...
 
Upvote 0
Have you considered using a named Formula instead.

Let me use a simpler example of the formula =A2+5

If you select B2 and define the name
Name: myFormula
RefersTo: =Sheet1!A2+5

and then put the =myFormula in B2 you should get the expected result.
Since, names follow the same absolute/relative referencing, putting =myFormula in B3 should return 5 more than A3 and =myFormula in D2 should return 5 five more than C2.

If you add $ to the definition, it will act accordingly.


This uses fewer resources than a UDF.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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