Excel User Functions
description
Transcript of Excel User Functions
![Page 1: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/1.jpg)
Excel User Functions
![Page 2: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/2.jpg)
Writing your own Excel user function
• Excel provides over 400 functions to users• But sometimes it would be very convenient to
have a function which is not provided • In our example (in workbook
UserFunctionDemo) , we have a function that finds the middle value of three numbers
• The first step is to create a module for the function
![Page 3: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/3.jpg)
Mac vs Windows
• The process on the Mac is almost exactly the same
• I’ve inserted some slides at the end to show how it looks on the Mac, and made a separate video
• The one difference I’ve found is that the user functions you write don’t show up in the function list the way they do on Windows
![Page 4: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/4.jpg)
A User Function goes in a Module
Instead of a UserForm, insert a Module
![Page 5: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/5.jpg)
Change the Module Name and Type the Code
Use the Name property in the property box
![Page 6: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/6.jpg)
The name of my function shows up in the list of completions provided by Excel
![Page 7: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/7.jpg)
Here you can see it in action…
![Page 8: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/8.jpg)
To Add A Description…
• Users find it helpful to see a description of what the function does
• To provide one, click the macros icon (continued…)
![Page 9: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/9.jpg)
Type your macro name at the top (it’s not there because it’s not a workbook macro)
![Page 10: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/10.jpg)
Click the Options button…
![Page 11: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/11.jpg)
Type a helpful description and click OK
If you give your macro a shortcut key, make sure it is not the same keyused for something else really useful like control X, V, C or similar
![Page 12: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/12.jpg)
Another nice example
• I got this one from the website exceltip.com• It was contributed by Martin Green, an Excel
consultant• The idea: If you give Excel’s Weekday function a
date, it will give you a number from 1 to 7 representing the day. You want to have a macro return a day name instead of a number
• This is also implemented in workbook UserFunctionDemo
![Page 13: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/13.jpg)
Here’s the code (most of it)Function DayName(inputDate As Date) As String Dim dayNumber As Integer 'we know this number is always small dayNumber = Weekday(inputDate, vbSunday) '*** Use Select Case to choose the dayname based on the number Select Case dayNumber Case 1 DayName = "Sunday" Case 2 DayName = "Monday"… Case 7 DayName = "Saturday" End Select End Function
![Page 14: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/14.jpg)
A few comments…
• Giving the type of the value returned, as we did here, seems to be optional, even with Option Explicit in force
Function DayName(inputDate As Date) As String
• The definition ends with End Function instead of End Sub, as it would with a sub procedure
• This is a nice example of where the Select Case conditional gives clear, readable code
• Weekday is an Excel function; the second argument is a constant that tells Excel we want 1 to represent Sunday
![Page 15: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/15.jpg)
Example for DayName
![Page 16: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/16.jpg)
HOW IT LOOKS ON MAC…
![Page 17: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/17.jpg)
Go into the editor and choose Insert Module
![Page 18: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/18.jpg)
Rename the Module using the Properties window
I named it MyFunction
![Page 19: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/19.jpg)
Type in the code and save
![Page 20: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/20.jpg)
In the Developer Tab, choose Macros…
Type the macro nameand choose Options, then type the description
![Page 21: Excel User Functions](https://reader036.fdocuments.in/reader036/viewer/2022081511/56816315550346895dd38e55/html5/thumbnails/21.jpg)
Go to Excel and Use Your Function!
I used my function Middle in a formula for cell A4.