Excel – How to iterate through a string and check the byte value of every character

excelfor-loopstringvba

Code I have:

cell_val = CStr(Nz(fld.value, ""))
Dim iter As Long
For iter = 0 To Len(cell_val) - 1 Step 1
    If Asc(Mid(cell_val, iter, 1)) > 127 Then
        addlog "Export contains ascii character > 127"
    End If
Next iter

This code doesn't work. Anyone know how to do this? I've simply got no idea with VB or VBA.

Best Solution

I believe your problem is that in VBA string indexes start at 1 and not at 0. Try the following:

For iter = 1 To Len(cell_val) 
    If Asc(Mid(cell_val, iter, 1)) > 127 Then
        addlog "Export contains ascii character > 127"
    End If
Next