How to iterate over a string and check the byte value for each character?

The code I have is:

        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 does not work. Does anyone know how to do this? I just have no idea about VB or VBA.

+5
source share
7 answers

I believe your problem is that in VBA row indices start at 1, 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
+12
source

With VBA, VB6, you can simply declare a byte array and assign it a string value, and it will be converted for you. Then you can just iterate over it like a regular array.

eg.

Dim b() as byte
Dim iter As Long
b = CStr(Nz(fld.value, ""))

For iter = 0 To UBound(b)
    if b(iter) > 127 then
        addlog "Export contains ascii character > 127"
    end if
next
+3
source

, , Nz addLog.

, 0 len() - 1. VBA 1 n.

 Dim cell_val As String
 cell_val = "øabcdæøå~!#%&/()"
 Dim iter As Long
 For iter = 1 To Len(cell_val)
    If Asc(Mid(cell_val, iter, 1)) > 127 Then
       'addlog "Export contains ascii character > 127"
       Debug.Print iter, "Export contains ascii character > 127"
    End If
 Next iter
+2

?;) , cell_val ? " 1" For, . ? , - ascii 127? - ?

0

AscW()

0

VB/VBA , , :

For iter = 1 To Len(cell_val)

step 1, .

0

?;) , cell_val ? " 1" For . , ? , ascii 127? - ?

, , vba , . , , cell_val . , , .

, , VBA 1, 0.

, , vba, , .

0

All Articles