A library of common Excel VBA functions that I have found useful over the years.
- Create a module in excel called
common
. - Copy the functions you need into the
common
module. - Call functions by using
common.functionName
.
Encodes string to Base64.
Input: String
Output: String
encoded to base64
Decodes binary to string. 2003 Antonin Foller, http://www.motobit.com
Input: Variant
as bianary data (ex. VT_UI1 | VT_ARRAY)
Output: String
Converts range or named range to delimited lists.
Input:
String
Worksheet Name (ex. "Sheet 1")String
Range Name (ex. "A1:B5" or "clientNames")String
Delimiter (ex. ";" or ", ")
Output: String
Delimited List (ex. "Hello, World,")
Count the number of items in an array that contain a value.
Input: Variant
Array (ex. [1,2, ,4])
Output: Integer
(ex. 3)
This function takes two strings of any length and calculates the Damerau-Levenshtein Distance between them. Damerau-Levenshtein Distance differs from Levenshtein Distance in that it includes an additional operation, called Transpositions, which occurs when two adjacent characters are swapped. Thus, Damerau-Levenshtein Distance calculates the number of Insertions, Deletions, Substitutions, and Transpositons needed to convert string1 into string2. As a result, this function is good when it is likely that spelling errors have occured between two string where the error is simply a transposition of 2 adjacent characters.
Author: Anthony Mancini, https://github.com/x-vba/xlib
Modified By: Justin Icenhour, 2021
Required Types:
common.CaseSensitivity
Required References:
Microsoft Scripting Library
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Integer
Distance (ex. "5")
Enable or disable events and screen updating on the application level.
Input: Boolean
Enable (ex. True or False)
Output: None
Finds queried value in a specified column and returns the row number where the query is found.
Required Functions:
common.getColumnLetter
Input:
String
Search Worksheet Name (ex. "Sheet 1")String
Search Term (ex. "foo")String
Search Column (ex. "A:A")
Output: Integer
Row Number
Finds queried value in a specified row and returns the column number where the query is found.
Required Functions:
common.getColumnLetter
Input:
String
Search Worksheet Name (ex. "Sheet 1")String
Search Term (ex. "foo")String
Search Row (ex. "1:1")
Output: Integer
Column Number
Configurable fuzzy find algorithm for string matching.
Required Types:
common.CaseSensitivity
Required References:
Microsoft Scripting Library
Required Functions:
common.originalMetric
common.damerau
common.hamming
common.levenshtein
common.sorensenDice
common.ngrams
common.tversky
common.uniqueArrayElements
common.jaccard
common.jaroWinkler
common.simpleMatching
common.min
common.max
Input:
String
Query (ex. "foo")Range
Search Range (ex. Range("A1:B5"))Worksheet
Search Sheet Name (ex. ThisWorkbook.Sheets("Sheet 1"))- Optional
CaseSensitivity
Case Sensitive (ex. CaseSensitivity.Sensitive) - Optional
Variant
Weights (ex. Array(1, .2, 3, 4, 5, .06, 7, 8, .009)) - Optional
Boolean
Tversky Symmetry (ex. True) - Optional
Variant
Tversky Weights (ex. Array(1, 2))
Output: String
Closest Matched Value
Finds specified column header on specified sheet and enters a list of printers available on the network into the column below the header.
Required Functions:
common.findQueryInRow
common.getColumnLetter
Input:
String
Destination Sheet (ex. "Sheet 1")String
Destination Column Header (ex. "Printer List")
Output: None
Returns the column letter for specified column number.
Input: Long
Column Number
Output: String
Column Letter
This function takes two strings of the same length and calculates the Hamming Distance between them. Hamming Distance measures how close two strings are by checking how many Substitutions are needed to turn one string into the other. Lower numbers mean the strings are closer than high numbers.
Author: Anthony Mancini, https://github.com/x-vba/xlib
Modified By: Justin Icenhour, 2021
Required Types:
common.CaseSensitivity
Required References:
Microsoft Scripting Library
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Integer
Distance (ex. "5")
Sends http POST or GET request and returns the response.
Input:
String
URL (ex. "https://api.insightly.com/v3.1/Contacts/")Boolean
Post (ex.True
orFalse
) - Optional
Output: String
HTTP Response
Calculate the Jaccard Similarity Coefficient.
Inspired By: DigitecGalaxus, C#
, https://github.com/DigitecGalaxus/Jaccard
Required Types:
common.CaseSensitivity
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Double
Coefficient (ex. ".1234")
Calculate the Jaro-Winkler distance.
Inspired By: jordanthomas, Javascript
, https://github.com/jordanthomas/jaro-winkler
Required Types:
common.CaseSensitivity
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Double
Distance (ex. ".1234")
Tools for using JSON with VBA.
VBA-JSON v2.3.1 - (c) Tim Hall - https://github.com/VBA-tools/VBA-JSON
JSONLib - Copyright (c) 2013, Ryo Yokoyama http://code.google.com/p/vba-json/
VBA-UTC v1.0.6 - (c) Tim Hall - https://github.com/VBA-tools/VBA-UtcConverter
Convert local date to ISO 8601 string.
Input: Date
UTC Local Date
Output: String
ISO Date
Convert dictionary, collection, or array to JSON.
Input:
Variant
Dictionary, Collection, or Array to be convertedVariant
Whitespace - "Pretty" print json with given number of spaces per indentation (Integer) or given stringLong
Current indentation (Default:0
)
Output: String
JSON
Convert local date to UTC date.
Input: Date
Local Date
Output: Date
UTC Date
Parse ISO 8601 date string to local date.
Input: String
ISO date string
Output: Date
Local Date
Convert JSON to dictionary or collection.
Input: String
JSON
Output: Object
Dictionary or Collection
Parse UTC date to local date.
Input: Date
UTC Date
Output: Date
Local Date
This function takes two strings of any length and calculates the Levenshtein Distance between them. Levenshtein Distance measures how close two strings are by checking how many Insertions, Deletions, or Substitutions are needed to turn one string into the other. Lower numbers mean the strings are closer than high numbers. Unlike Hamming Distance, Levenshtein Distance works for strings of any length and includes 2 more operations. However, calculation time will be slower than Hamming Distance for same length strings, so if you know the two strings are the same length, its preferred to use Hamming Distance.
Author: Anthony Mancini, https://github.com/x-vba/xlib
Modified By: Justin Icenhour, 2021
Required Types:
common.CaseSensitivity
Required References:
Microsoft Scripting Library
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Integer
Distance (ex. "5")
Locks or unlocks all sheets, unless a sheet is provided then only that sheet will be locked or unlocked.
Input:
Boolean
Locked (ex.True
orFalse
)Worksheet
Single Sheet
Output: None
This function takes multiple numbers or multiple arrays of numbers and returns the max number. This function also accounts for numbers that are formatted as strings by converting them into numbers.
Author: Anthony Mancini, https://github.com/x-vba/xlib
Input: Variant
Numbers (ex. Array(1, 3, 5, 5, 9, 9.5))
Output: Double
Max Value (ex. 9.5)
This function takes multiple numbers or multiple arrays of numbers and returns the min number. This function also accounts for numbers that are formatted as strings by converting them into numbers.
Author: Anthony Mancini, https://github.com/x-vba/xlib
Input: Variant
Numbers (ex. Array(.5, 1, 3, 5, 5, 9, 9.5))
Output: Double
Min Value (ex. .5)
Determine the grams of a given length for a string. (ex. nGrams("Hello World", 2) = ("He", "el", "ll", "lo", "o ", " W", "Wo", "or", "rl", "ld")
Input:
String
text (ex. "foo")
Output: Variant
nGram (ex. Array("He", "el", "ll", "lo", "o ", " W", "Wo", "or", "rl", "ld"))
Converts any single-digit number to text.
Input: String
Digit (ex. "5")
Output: String
Text (ex. "Five")
String metric.
Required Types:
common.CaseSensitivity
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Double
Metric (ex. ".5")
Removes duplicate values from specified range.
Input:
Worksheet
Origin Worksheet (ex.ThisWorkbook.Sheets("Sheet 1")
)Range
Origin Range (ex.ThisWorkbook.Sheets("Sheet 1").Range("A1:B5")
)
Output: Scripting.Dictionary
of which the keys are the values from the range with the duplicates removed
Removes the specified leading string if it appears at the beginning of the whole string. You can determine the output to be text or an excel formula to achieve the same result.
Input:
String
Leading string to be removed (ex. "foo ")String
Whole string (ex. "foo bar")Boolean
Return Excel formula or text (ex.True
orFalse
)
Output:
String
Text (ex. "bar")String
Formula (ex. "IF(LEFT(" &whole
& ",LEN("&lead
&"))=""" &lead
& """,RIGHT(" &whole
& ",LEN(" &whole
& ")-LEN(" &lead
& ")), " &whole
& ")")
Takes any shape, chart, or other object and exports it to you desktop as a PNG
file.
Input:
String
Name of worksheet where the object residesString
Name of objectString
File name of exported image
Output: None
Calculate the simple matching metric.
Required Types:
common.CaseSensitivity
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Double
Metric (ex. ".1234")
Get the edit-distance according to Dice between two values.
Inspired By: words, Javascript
, https://github.com/words/dice-coefficient
Required Types:
common.CaseSensitivity
Required References:
Microsoft Scripting Library
Required Functions:
common.ngrams
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)
Output: Integer
Distance (ex. "5")
Takes a number and spells it out in words eg. 1
to "One".
Required Functions:
common.oneDigitNumberToText
common.twoDigitNumberToText
common.threeDigitNumberToText
Input:
Variant
Number to Spell (ex.123.01
)String
Name of Currency (ex. "Dollars")
Output: String
Text (ex. "One Hundred Twenty Three Dollars And One Cent")
Encodes string to binary. 2003 Antonin Foller, http://www.motobit.com
Input: String
Text (ex. "Hello World")
Output: Binary
(ex. "01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100")
Converts any three-digit number to text.
Required Functions:
common.oneDigitNumberToText
common.twoDigitNumberToText
Input: String
Three digit number (ex. "123")
Output: String
Number as text (ex. "One Hundred Twenty Three")
Converts string to camel case.
Required Functions:
common.toPascalCase
Input: String
Text (ex. "Hello World")
Output: String
Text (ex. "helloWorld")
Converts string to pascal case.
Input: String
Text (ex. "hElLo WoRlD")
Output: String
Text (ex. "HelloWorld")
Converts any two-digit number to text.
Required Functions:
common.oneDigitNumberToText
Input: String
Two digit number (ex. "42")
Output: String
Text (ex. "Forty Two")
Computes the Tversky index between two sequences. For alpha = beta = 0.5, the index is equal to Dice's coefficient. For alpha = beta = 1, the index is equal to the Tanimoto coefficient.
Inspired By: compute-io, Javascript
, https://github.com/compute-io/tversky-index
Required Types:
common.CaseSensitivity
Required Functions:
common.uniqueArrayElements
Input:
String
String1 (ex. "foo")String
String2 (ex. "bar")CaseSensitive
Case Sensitivity (ex. CaseSensitive.Sensitive)- Optional
Boolean
Symmetry (ex. True) - Optional
Double
String1 Weight (ex. .5) - Optional
Double
String2 Weight (ex. .5)
Output: Double
Index (ex. ".1234")
Computes the unique elements of an array.
Required References:
Microsoft Scripting Library
Input: Variant
Array (ex. Array(1,1,2,3))
Output: Variant
Array (ex. Array(1,2,3))