a) Approach via Byte array
In addition to the valid solutions above and for the sake of the art another approach using a byte array:
Sub ExampleCall()
Dim s As String: s = "100000010000100000"
Debug.Print s & " ~> " & getRole(s) ' ~> Admin, Viewer, TA
End Sub
Help function getRole()
Function getRole(code As String) As String
'[0]define identifying role position in code string (e.g. "100000010000100000")
Dim roles As Variant, b() As Byte
roles = Array(0, "Admin", 2, 3, 4, 5, 6, "TE", "Viewer", 9, "DEV", "TL", 12, "TA", 14, 15, 16, 17, 18)
'[1]assign code string to byte array (generates two elements per character)
b = code
'[2]get the "1" positions
Dim i As Long, role As String
For i = LBound(b) To UBound(b) - 1 Step 2 ' check the 1st element of each char pair
If b(i) = Asc("1") Then role = role & roles(Int(i / 2) + 1) & ", "
Next
role = Left(role, Len(role) - 2)
'[3]return function result
getRole = role
End Function
~~~~~~~~~~~~~~~~~~~~ Edit ~~~~~~~~~~~~~~~~~~~
b) Alternative via FilterXML() (Excel vers 13+)
In order to complete the whole range of different approaches, I demonstrate how to use the WorksheetFunction FilterXML() (available in versions 13+). As there is no need to bind to a library, you may find this simple example as an alternative to dictionaries.
Sub ExampleCall2()
Dim code As String: code = "100000010000100000"
Debug.Print decode(code)
End Sub
Help functions decode() and XMLRole()
Function decode(code) As String
Dim pos As Long, cnt As Long, isReady As Boolean
ReDim tmp(1 To Len(code))
Do While Not isReady
pos = InStr(pos + 1, code, "1")
If pos = 0 Then
isReady = True
Else
cnt = cnt + 1: tmp(cnt) = XMLRole(pos)
End If
Loop
ReDim Preserve tmp(1 To cnt)
decode = Join(tmp, ", ")
End Function
Function XMLRole(code) As String
Const wellformed As String = "<roles><r t='Admin'>1</r><r t='TE'>7</r><r t='Viewer'>8</r><r t='DEV'>10</r><r t='TL'>11</r><r t='TA'>13</r></roles>"
XMLRole = WorksheetFunction.FilterXML(wellformed, "//r[.=" & code & "]/@t")
End Function
Short hints to XPath
XPath strings (here: "//r[.=" & code & "]/@t") allow to search node or attribute contents enriched by conditions (here e.g.: [.=11] pointing to a numeric content of e.g. 11) within a wellformed node structure (comparable to html). Further sub nodes or attributes are added by / to the whole term (here: attribute content of /@t (symbolising type, e.g. 'TL') after a r role node; btw the double //r indicates a role search of any r anywhere after the root element (here: roles).