Excel VBA: Base64 Encoding, Easy

Excel VBA: Base64 Encoding, Easy

Base64 encoding and decoding of text is a common requirement, yet Excel and VBA have no native function for doing so.

Here is the most efficient and streamlined method I've conjured to encode text to Base64:

Function EncodeBase64(text$)
    Dim b
    With CreateObject("ADODB.Stream")
        .Open: .Type = 2: .Charset = "utf-8"
        .WriteText text: .Position = 0: .Type = 1: b = .Read
        With CreateObject("Microsoft.XMLDOM").createElement("b64")
            .DataType = "bin.base64": .nodeTypedValue = b
            EncodeBase64 = Replace(Mid(.text, 5), vbLf, "")
        End With
        .Close
    End With
End Function

And the sister function to decode:

Function DecodeBase64(b64$)
    Dim b
    With CreateObject("Microsoft.XMLDOM").createElement("b64")
        .DataType = "bin.base64": .text = b64
        b = .nodeTypedValue
        With CreateObject("ADODB.Stream")
            .Open: .Type = 1: .Write b: .Position = 0: .Type = 2: .Charset = "utf-8"
            DecodeBase64 = .ReadText
            .Close
        End With
    End With
End Function

Note that both of these functions support ASCII and it's extension, Unicode UTF-8.

So now you can easily and efficiently encode even tough text like the following.

யாமறிந்த மொழிகளிலே தமிழ்மொழி போல்

If the above text was in A1, then:

[a2] = EncodeBase64([a1])

...would assign the following text to cell A2:

4K6v4K6+4K6u4K6x4K6/4K6o4K+N4K6kIOCuruCviuCutOCuv+CuleCus+Cuv+CusuCvhyDgrqTgrq7grr/grrTgr43grq7gr4rgrrTgrr8g4K6q4K+L4K6y4K+N

And to return the encoded text back to the original:

[a3] = DecodeBase64([a2])

And cell A3 will contain the original text:

யாமறிந்த மொழிகளிலே தமிழ்மொழி போல்

Again, these functions will work on normal ASCII text as well as Unicode UTF-8. As a side note, realize that worksheet cells will fully display Unicode, but the VBA environment, including the VB Editor, MsgBoxes and Userforms are all ANSI and will not display Unicode.

Let me know your thoughts in the comments.









How can i change length of encoded string? For example, i need to encode into 30 characters length 4 digits number with sign "=" at the end.

Like
Reply

Adding this and saving it into excel is causing my AntiVirus to trigger. Any ideas why?

Like
Reply

Thanks for this. Was looking for a better way to do this.

Like
Reply

Daniel, nice article. Tableau uses Base64 encoding to store images used as so called Custom Shapes in a Tableau workbook. A few years back, I created a little VBA based Excel-tool to extract these images from any given Tableau workbook in order to be able to re-use in other Tableau workbooks or elsewhere. The Excel tool is open and so is the VBA-code. Here is the link to the blog post, if you are interested (download link to the Excel workbook is at the end of the post): https://www.clearlyandsimply.com/clearly_and_simply/2014/05/extract-custom-shapes-from-a-tableau-workbook.html

To view or add a comment, sign in

More articles by Daniel Ferry

  • To CSV Or Not To CSV

    The comma-separated values (CSV) file predates the personal computer and was first used by IBM Fortran in 1972! If you…

    31 Comments
  • Excel VBA: Parse JSON, Easily

    Data come in many formats. Excel is great at some, such as xls*, xml, csv, etc.

    7 Comments
  • Excel: Descriptive Statistics, Easy

    The first thing many analysts do when they get their hands on a new data-set in Excel is to fire up the Data Analysis…

    6 Comments
  • Excel VBA: Create GUIDs, Easily

    Globally Unique Identifiers are ubiquitous in the computing world. Your Windows Registry is full of them, because COM…

    3 Comments
  • Excel VBA: Clean Data, Easily

    It's common to receive data that needs to be cleaned of non-printing characters before it can be processed further. The…

    6 Comments
  • Excel VBA: Windows Clipboard, Easy

    When Joel Spolsky created VBA for Excel it was an absolute boon for developers. VBA is based on VB6, the venerable…

    11 Comments
  • Excel VBA: Get Unique Values Easily

    There are many ways to get distinct values from a worksheet column when working in VBA. Using the Advanced Filter or…

    24 Comments

Explore content categories