VBA Base36 Encoding: A Practical Implementation
Looking for a simple VBA Base36 encoding solution? Base36 encoding (also known as sexatrigesimal) is a handy way to represent numbers using a character set of 36 symbols. In this guide, I’ll share a straightforward implementation for Visual Basic developers.
Base36 encoding uses digits 0-9 and letters A-Z to represent numbers in a more compact form. This encoding system is useful for creating shorter identifiers and URLs while maintaining readability.
Why Use VBA Base36 Encoding?
Base36 encoding offers several advantages for Visual Basic programmers:
- Creates shorter, human-readable strings from large numbers
- Useful for generating unique IDs in databases
- Helps create cleaner URLs and references
- Easy to implement in VBA/VB6 environments
VBA Base36 Encoding and Decoding Functions
Below is a clean implementation of Base36 encoding and decoding functions in VBA. These functions convert between integers and Base36 strings efficiently.
'Convert positive integer to a base36 string.
Function base36encode(ByRef number As Long) As String
Dim alphabet As String
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If number = 0 Then
base36encode = "0"
Exit Function
End If
base36encode = vbNullString
Do While number <> 0
base36encode = Mid(alphabet, number Mod 36 + 1, 1) & base36encode
number = number \ 36
Loop
End Function
'Convert base36 string to positive integer.
Function base36decode(ByRef base36 As String) As Long
Dim alphabet As String
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
base36decode = InStr(1, alphabet, Right(base36, 1), vbTextCompare) - 1 'get the last character
For i = Len(base36) - 1 To 1 Step -1
base36decode = base36decode + 36 ^ (Len(base36) - i) * (InStr(1, alphabet, Mid(base36, i, 1), vbTextCompare) - 1)
Next i
End Function
How to Use These VBA Base36 Functions
Using these functions is simple. Just add them to your VBA module and call them as needed:
'Encoding example
Dim myNumber As Long
Dim encodedString As String
myNumber = 12345
encodedString = base36encode(myNumber) 'Returns "9IX"
'Decoding example
Dim decodedNumber As Long
decodedNumber = base36decode("9IX") 'Returns 12345
Applications of Base36 Encoding
Base36 encoding can be used in many practical scenarios:
Base36 encoding is particularly useful when you need to create compact, human-readable identifiers from numeric data. It strikes a balance between brevity and readability.
Common applications include:
| Application | Benefit |
| URL shortening | Creates compact, readable URLs |
| Database IDs | Shortens numeric identifiers |
| Reference codes | Creates readable alphanumeric codes |
For more information about different encoding systems and their applications, check out resources like Microsoft’s Visual Basic documentation or TutorialsPoint’s VBA tutorials.
Have you implemented Base36 or other encoding systems in your VBA projects? Share your experiences in the comments!