Skip to: Site menu | Main content

Login

Name: 
Password:
Remember me?
Register

Creating UDF's

written by Mark Rowlinson - Last updated Dec 2006

You write functions by adding a module to a project and creating a public function:

Public Function MyFunctionName(ArgList As Type) As Type 
     
    'workings 
     
    MyFunctionName=Result 
     
End Function 

You'll notice that to return the result you asign the value you wish to return to the function name. This is the same as for any function in Visual Basic. For example a simple function that add's 1 to a value would be:
 
Public Function AddOne(Value As Single) As Single 
    AddOne=Value+1 
End Function 
 

You can pass any type of data to the function depending on the type of data that you require. For instance if you are working with dates you could have a function:
 
Public Function 1DayLater(DateValue As Date) As Date 
    1DayLater=DateValue+1 
End Function 
 

You can also accept an excel range as an argument. For example the above function could be rewritten as:
 
Public Function 1DayLater(DateRange As Range) As Date 
    1DayLater=DateRange.Value+1 
End Function