Work around via HTML
Just in addition to the valid solutions above: I found an easy work around using IE HTML content, as HTML is not distinguishing between lower and higher code block sets; the function below simply returns the interpreted inner html:
Example call writing a padlock symbol e.g. to cell A1
[A1] = GetUnicode("1F512")
[1] Function GetUnicode() - via InternetExplorer
Function GetUnicode$(ByVal CharCodeString$)
' Purpose: get Unicode character via any valid unprefixed hex code string
' Note: late bound InternetExplorer reference
Dim Ie As Object
Set Ie = CreateObject("InternetExplorer.Application")
With Ie
.Visible = False
.Navigate "about:blank"
.document.body.innerhtml = "&#x" & CharCodeString & ";" ' prefixing HTML code
GetUnicode = .document.body.innerhtml
.Quit
End With
End Function
[2a] Alternative Function GetUnicode() - via XMLDom (Edit 5/12 2019)
This represents a host agnostic approach using XMLDom. Citing Wikipedia
"The Document Object Model (DOM) is a cross-platform and language-independent application programming interface that treats an XML document as a tree structure wherein each node is an object representing a part of the document. "
Similar to the IE approach the Unicode entity consists of the numeric (hex) prefix &#x
+ num + ;
. Generally I love XML as it allows generally a more flexible coding via its individual node and sub-node references; this example only demonstrates the simplest way to give an idea.
Function getUnicode$(ByVal CharCodeString$)
' Purpose: get Unicode character via any valid unprefixed hex code string
' Note: late bound MSXML2 reference using XMLDom
Dim XmlString$
XmlString = "<?xml version=""1.0"" encoding=""UTF-8""?><root><symbol>&#x" _
& CharCodeString & ";</symbol></root>"
With CreateObject("MSXML2.DOMDocument.6.0")
.ValidateOnParse = True
.Async = False
If .LoadXML(XmlString) Then
getUnicode = .DocumentElement.SelectSingleNode("symbol").Text
End If
End With
End Function
[2b] Further approach using FilterXML
- late edit as of 12/29 2019
The WorksheetFunction FilterXML
added in Excel 2013 allows to reformulate and shorten the above code as follows:
Function getUnicode$(ByVal CharCodeString$)
' Purpose: get Unicode character via any valid unprefixed hex code string
' Note: the FilterXML() function was introduced by Version 2013
' Help: https://learn.microsoft.com/de-de/office/vba/api/excel.worksheetfunction.filterxml
Dim XmlString As String
XmlString = "<?xml version=""1.0"" encoding=""UTF-8""?><root><symbol>&#x" _
& CharCodeString & ";</symbol></root>"
getUnicode = Application.WorksheetFunction.FilterXML(XmlString, "//symbol")
End Function
Addendum (5/2 2021)
Using the above FilterXML()
function you can also omit the XML declaration <?xml version=""1.0"" encoding=""UTF-8""?>
:
Excel and generally any XML parser assumes that the encoding is UTF-8 or UTF-16 if an XML document lacks encoding specification (unless the encoding has already been determined by a higher protocol). So you could even truncate the above function to
Function getUnicode$(ByVal hex$)
getUnicode = Application.WorksheetFunction.FilterXML("<r><s>&#x" & hex & ";</s></r>", "//s")
End Function
Note that any node names can be used instead of the above tags <r>
(for <root>
) or s
(for <symbol>
).