Excel - UDF
Function to Check if File or Directory (Folder) exists
Submitted by Ken Puls on Sun, 2006-09-24 21:51. Excel - UDF | Excel - VBAIntroduction:
When creating routines, it is commonly helpful to check if a file or directory (folder) exists before attempting to perform some action. Searching the internet will yield several examples of doing either, but this is a routine that I wrote in order to have one routine to test either.
Macro Purpose:
- Check if the supplied path to a file or directory (folder) exists.
Examples of where this function shines:
- When you need to check if a file OR directory exists before you attempt to so something with it (such as load a file).
Function To Get File Name From Specific Directory
Submitted by Ken Puls on Mon, 2006-07-24 07:18. Excel - UDF | Excel - VBAIntroduction:
This is one of my favourite functions to use in my projects. While VB's standard GetOpenFilename works great if you are in the correct directory, this saves my users time as I can tell it exactly where I want the dialog box to start looking... a very handy thing if you are opening files from different paths.
Macro Purpose:
- Function to ask the user for a file name, but opening the window at a specific directory.
Examples of where this function shines:
- When you need to give the users options to open files from a variety of paths.
Formula To Average Letters
Submitted by Ken Puls on Tue, 2006-01-17 00:00. Excel - Formulas | Excel - UDF | Excel - VBAFormula Purpose:
- This function takes a series of letters and returns the average of them, i.e., the average of A, B, C is B.
Examples of where this function shines:
- The only application that I can see for this formula is for averaging grades. If you know another, please click here and send me an email to let me know!
Formula Weakness(es):
- It only works on a single contiguous range on a worksheet, not multiple ranges.
- Must remember to enter as an array formula.
- Only operates on the first character in each cell.
User Defined Function To Show Formulas In A Cell
Submitted by Ken Puls on Wed, 2006-01-11 00:00. Excel - Formulas | Excel - UDF | Excel - VBAMacro Purpose:
- Function to show the formula in another cell as text.
Examples of where this function shines:
- In Excel's View options (Tools|Options|View), you can check a box to show all formulas on the worksheet as formulas, rather than their resulting values. The problem is that this is worksheet level, and sometimes that may not be desireable. This function allows you to show the formulas on the worksheet if you desire.
- As an example of a practical use for this function, I have used it throughout this site to show what formulas I use in my examples.
Macro Weakness(es):
- None identified at this time.
Versions Tested:
This function has been tested with Excel 97, and Excel 2003, and should also work with Excel 2000 and Excel 2002 (XP) without any modifications.
VBA Code Required:
- Place the following code in a standard module of the workbook you wish to use it in.
Function showformula(rng As Range) 'Author : Ken Puls (www.excelguru.ca) 'Function Purpose: To show a range's formula in a cell If rng.HasArray = True Then showformula = "{" & rng.Formula & "}" Else showformula = rng.Formula End If End Function
How to use the code:
- Enter the formula in a worksheet cell in the format =SHOWFORMULA(Range)
- Click on the formulas in the example below to see how the results were generated.
(The actual formula used will change in the Formula Bar.)
User Defined Function To Sum And Round To Two Decimal Places
Submitted by Ken Puls on Tue, 2006-01-10 00:00. Excel - Formulas | Excel - UDF | Excel - VBAMacro Purpose:
- To round a Sum formula to two decimal places
Examples of where this function shines:
- When you are feeling really lazy, and don't want to type =Round(Sum(A1:A3),2), you can just type =Sumr(A1:A3)
Macro Weakness(es):
- Will calculate slower than the nested Round and Sum combination. Performance degridation most likely won't be noticed if it is only used a few times, but if hundreds of formulas are used, it may slow the calculation down noticeably.
- Only accepts range arguments, not values.
Function to return various Environment Names
Submitted by Ken Puls on Tue, 2005-01-04 21:38. Excel - Formulas | Excel - UDF | Excel - VBAFunction Purpose:
This function can be used to retrieve one of three types of usernames, and can be used in a worksheet cell or in VBA. The usernames that it can return are:
- Microsoft Office username (default) as defined in Tools|Options|General
- Microsoft Windows logon name
- Computer name (as defined in your system properties)
Examples of where this function shines:
- When designing templates for use by multiple users
- Automate the task of entering the user's name(s) on the worksheet!
- When building macros
- Have your code execute a certain way depending upon the logged in user, and/or the machine that they are logged in on!
Function Weakness(es):
- The Microsoft Office username must be entered correctly (defined in Tools|Options|General) or it will return incorrect results
- Running in an environment where people log in sharing a Windows profile doesn't work well, as the computer can't identify who the user really is
- Must use the Application.Volatile keywords to force recalculation, or the function won't recalculate when the template is opened (unless force-coded to do so.)
Versions Tested:
This function has been tested with Excel 97, and Excel 2003, and should also work with Excel 2000 and Excel 2002 (XP) without any modifications
VBA Code Required:
The following should be copied into a standard code module:
Option Explicit
Function GetName(Optional NameType As String) As String
'Author : Ken Puls (www.excelguru.ca)
'Function purpose: To return the following names:
'Defaults to MS Office username if no parameter entered
'Formula should be entered as =GetName([param])
'For Name of Type Enter Text OR Enter #
'MS Office User Name "Office" 1 (or leave blank)
'Windows User Name "Windows" 2
'Computer Name "Computer" 3
'Force application to recalculate when necessary. If this
'function is only called from other VBA procedures, this
'section can be eliminated. (Req'd for cell use)
Application.Volatile
'Set value to Office if no parameter entered
If Len(NameType) = 0 Then NameType = "OFFICE"
'Identify parameter, assign result to GetName, and return
'error if invalid
Select Case UCase(NameType)
Case Is = "OFFICE", "1"
GetName = Application.UserName
Exit Function
Case Is = "WINDOWS", "2"
GetName = Environ("UserName")
Exit Function
Case Is = "COMPUTER", "3"
GetName = Environ("ComputerName")
Exit Function
Case Else
GetName = CVErr(xlErrValue)
End Select
End Function
Referencing The Formula:
- The formula can be referenced in worksheet cells as shown in the example below:
| |||||||
| |||||||
| Parameter Supplied | |||||||
| Using Words | Using #'s | ||||||
|
| Name | Formula is: | Results Are: | Formula is: | Results Are: | ||
| Microsoft Office | =GetName("office") | Ken Puls | =GetName(1) | Ken Puls | |||
| Windows | =GetName("windows") | Ken Puls | =GetName(2) | Ken Puls | |||
| Computer | =GetName("computer") | KEN | =GetName(3) | KEN | |||
| No parameter given | =GetName("") | Ken Puls | =GetName() | Ken Puls | |||
| |||||||
[HtmlMaker 2.42] To see the formula in the cells just click on the cells hyperlink or click the Name box


