FilleFrella44

New Member
Joined
Oct 11, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello everyone!

I've recently gotten to understand that you're able to create your own functions by taking an Excel-workbook, adding some VBA "Functions" to it in modules, and then save and load it as an Add-in into Excel.

Now this is so amazing, a real game-changer. But, what I want to know is: What are your favorite UDFs? Want to share?

Regards,
FilleFrella
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
My favorite is sumbycolor:
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Application.Volatile
Dim cSum As Double

Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
If cl.Interior.ColorIndex = ColIndex Then
cSum = WorksheetFunction.Sum(cl, cSum)
End If
Next cl
SumByColor = cSum
End Function
 
Upvote 0
Although there are formulae for expressing numbers as ordinals, the resulting output is a string - which makes it had to evaluate dates where the day is expressed as an ordinal.

The following two sub-routines, placed in the relevant worksheet’s code module, together with the third sub-routine and the accompanying function in a standard module, will automatically apply ordinal formatting to values entered into a range named "Ordinal":
Code:
Private Sub Worksheet_Calculate()
Ordinals ActiveSheet.Range("Ordinal")
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Ordinals Target
End Sub

Public NmRng As String
Sub Ordinals(ByVal Target As Range)
Dim oCell As Range
If NmRng <> "" Then _
  ActiveWorkbook.Names.Add Name:="Ordinal", _
  RefersTo:=ActiveWorkbook.Names.Item("Ordinal") & "," & NmRng
If Intersect(Target, ActiveSheet.Range("Ordinal")) Is Nothing Then Exit Sub
On Error Resume Next
For Each oCell In Target
  If IsNumeric(oCell.Value) Then oCell.NumberFormat = OrdFmt(oCell.Value)
Next
End Sub

Function OrdFmt(ByVal Num As Long) As String
Dim Cell As Range
If IsNumeric(Cell.Value) Then
  OrdFmt = "#""" & Mid$("thstndrdthththththth", 1 - 2 * _
    ((Cell.Value) Mod 10) * (Abs((Cell.Value) Mod 100 - 12) > 1), 2) & """"
End If
End Function
One limitation of User-Defined Functions (UDFs) is that they can only change the value displayed in the cell that contains the formula. This means you can’t use the UDF to change a cell’s number format or add it to a named range. They can, however, set a variable's properties, and this provides a workaround. By setting the ‘NmRng’ variable above, you can have a UDF that indirectly adds the target cell's address to the named range and exploit the 'Worksheet_Change' and 'Worksheet_Calculate' events to apply ordinal number formatting to a cell with a formula like: =OrdVal(A1):
Code:
Function OrdVal(ByVal Num As Long) As Long
Application.Volatile
NmRng = ActiveSheet.Name & "!" & Selection.Address
If InStr(ActiveWorkbook.Names.Item("Ordinal"), NmRng) <> 0 Then NmRng = ""
OrdVal = Num
End Function
Alternatively, the UDF below can use a formula like =Ordinal (A1) or =Ordinal(37) for any cell on any worksheet to which the event-driven code is attached.
Code:
Function Ordinal(ByVal Num As Long) As Long
Ordinal = Num
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
On Error Resume Next
For Each Cell In ActiveSheet.UsedRange
  If UCase(Cell.Formula) Like "=ORDINAL(?*)" Then
    Cell.NumberFormat = "#,#""" & Mid$("thstndrdthththththth", 1 - 2 * _
    ((Cell.Value) Mod 10) * (Abs((Cell.Value) Mod 100 - 12) > 1), 2) & """"
  End If
Next Cell
End Sub
The underlying values remain available for use as numbers in other formulae.

Of course, if only a limited range of cells in a large workbook can have this kind of formula, it would be wise to narrow the event's target range to just that. The above approach also allows a mix of ordinal and ordinary values to coexist in the target range.

Note: Decimal values expressed as ordinals are based on the nearest integer. As well, when driven by the Ordinal Function, the values are converted to strings.
 
Upvote 0
Cool! I've implemented the SumByColour one, and I'll make sure to keep the Ordinal-input in mind!
 
Upvote 0
In case you're not aware of it, for that function to be available in any Workbook - new or existing, the VBA has to be in your PERSONAL.XLSM (or PERSONAL.XLSB) file in the C:\Users\USERID\AppData\Roaming\Microsoft\Excel\XLSTART folder. I searched the page for personal and didn't find it, so thought you might not know.
The XLSB file type is a compressed "Binary" XLSX of XLSM file - that is it can hold a Macro, and other than being compressed is identical to the other two types.
 
Upvote 0
The contents of the compressed folder are different formats. In an xlsb the sheets are all stored as binaries whereas in the xlsx/xlsm they are stored in xml format.
 
Upvote 0
The contents of the compressed folder are different formats. In an xlsb the sheets are all stored as binaries whereas in the xlsx/xlsm they are stored in xml format.
Could it be that it can reduce the file size by almost 50%?
12/09/2022 01:10 PM 6,068,584 PQB08Append3Tables.xlsb
12/21/2021 08:49 AM 11,997,106 PQB08Append3Tables.xlsx
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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