Question : Character Position(s) in a string using VBA

I have a string in VBA: (000101101) etc. I want a function that will tell me the string locations of the (1's). The above example it would provide 6 4 3 1.  It sounds simple.  Ive tried Instr() and a number of others.

Answer : Character Position(s) in a string using VBA

Function positions(full As String, tofind As String) As String
    Dim i As Integer, j As Integer
    positions = ""
    j = Len(full)
    For i = 1 To Len(full)
        If Mid(full, i, 1) = tofind Then positions = positions & CStr(1 + j - i) & " "
    Next i
End Function

Sub test()
    Dim s As String
    s = positions("000101101", "1")
    MsgBox s
End Sub
Random Solutions  
 
programming4us programming4us