Method 1 – Apply an Excel VBA Code for a HTML Decode String
Steps:
- Go to the Developer tab from the ribbon.
- Click on Visual Basic from the Code category to open the Visual Basic Editor. Or, press Alt + F11.
- Click on Module from the Insert drop-down menu bar.
- This will create a Module in your workbook.
- Copy and paste the VBA code shown below into the module.
VBA Code:
Public Function HtmlDecode(StringToDecode As Variant) As String
Set HFile = CreateObject("htmlfile")
Set a = HFile.createElement("T")
a.innerHTML = StringToDecode
HtmlDecode = a.innerText
End Function
- Press Ctrl + S to save the file.
- Go back to the sample Excel sheet.
- Insert the following formula into the result. Use your own cell reference for the source if you’re making a table from scratch.
=HtmlDecode(B5)
- Press the Enter key.
- Drag the Fill Handle icon down to duplicate the formula over the range or double-click on the plus (+) symbol.
- You will see the results.
Read More: How to Change Encoding in Excel
Method 2 – Create a User Defined Function in Excel to Decode Base64
Steps:
- Open a VBA Module (see Method 1 for instructions if needed).
- Copy and paste the following VBA code into the module.
VBA Code:
Function Decoding(b64$)
Dim i
With CreateObject("Microsoft.XMLDOM").createElement("b64")
.DataType = "bin.base64": .Text = b64
i = .nodeTypedValue
With CreateObject("ADODB.Stream")
.Open: .Type = 1: .Write i: .Position = 0: .Type = 2: .Charset = "utf-8"
Decoding = .ReadText
.Close
End With
End With
End Function
- Hit Ctrl + S.
- Return to the Excel sheet now.
- Put the following formula into the result cell, where the reference in the formula is the cell with the text you wish to decode:
=Decoding(B5)
- Hit the Enter key.
- To copy the formula over the range, drag the Fill Handle symbol downward. Alternatively, double-click the plus (+) sign to AutoFill the range.
- You can see the results.
How to Encode Base64 in Excel
Steps:
- Access the Visual Basic Editor by pressing Alt + F11.
- Pick the Module from the drop-down box under Insert.
- Copy this code in the module.
VBA Code:
Function Encoding(text$)
Dim i
With CreateObject("ADODB.Stream")
.Open: .Type = 2: .Charset = "utf-8"
.WriteText text: .Position = 0: .Type = 1: i = .Read
With CreateObject("Microsoft.XMLDOM").createElement("b64")
.DataType = "bin.base64": .nodeTypedValue = i
Encoding = Replace(Mid(.text, 5), vbLf, "")
End With
.Close
End With
End Function
- Save the file by pressing Ctrl + S on your keyboard.
- Go back to the worksheet and insert the following formula in a cell where you want the code. The reference is the cell you want to encode.
=Encoding(B5)
- Press the Enter key.
- Drag the Fill Handle icon to copy the formula over the range or double-click on the plus (+) sign.
Things to Remember
- While using VBA code make sure you save your file with the .xlsm extension.
Download the Practice Workbook
You can download the workbook and practice with it.
Related Articles
- [Fixed!] CSV UTF 8 Not Available in Excel
- How to Check Encoding of Excel File
- How to Apply UTF 8 Encoding on CSV File in Excel
<< Go Back to Excel Encoding | Excel Files | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!