<p>Code I have:</p>

<pre><code>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)) &gt; 127 Then
        addlog "Export contains ascii character &gt; 127"
    End If
Next iter
</code></pre>

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

[{'answer_id': 80457, 'author': 'jan.vdbergh', 'author_id': 9540, 'author_profile': 'https://Stackoverflow.com/users/9540', 'pm_score': 5, 'selected': True, 'text': '<p>I believe your problem is that in VBA string indexes start at 1 and not at 0. Try the following:</p>\n\n<pre><code>For iter = 1 To Len(cell_val) \n    If Asc(Mid(cell_val, iter, 1)) &gt; 127 Then\n        addlog "Export contains ascii character &gt; 127"\n    End If\nNext\n</code></pre>\n'}, {'answer_id': 80460, 'author': 'Per Hornshøj-Schierbeck', 'author_id': 11619, 'author_profile': 'https://Stackoverflow.com/users/11619', 'pm_score': 0, 'selected': False, 'text': "<p>Did you debug it? ;) Are you sure the cell_val is not empty? Also you don't need the 'Step 1' in the For loop since it's default. Also what do you expect to acomplish with your code? It logs if any ascii values are above 127? But that's it - there is no branching depending on the result?</p>\n"}, {'answer_id': 80462, 'author': 'Scott Evernden', 'author_id': 11397, 'author_profile': 'https://Stackoverflow.com/users/11397', 'pm_score': 0, 'selected': False, 'text': '<p>Try AscW()</p>\n'}, {'answer_id': 80472, 'author': 'paxdiablo', 'author_id': 14860, 'author_profile': 'https://Stackoverflow.com/users/14860', 'pm_score': 0, 'selected': False, 'text': "<p>VB/VBA strings are based from one rather than zero so you need to use:</p>\n\n<pre><code>For iter = 1 To Len(cell_val)\n</code></pre>\n\n<p>I've also left off the <code>step 1</code> since that's the default.</p>\n"}, {'answer_id': 80489, 'author': 'vzczc', 'author_id': 224, 'author_profile': 'https://Stackoverflow.com/users/224', 'pm_score': 2, 'selected': False, 'text': '<p>Your example should be modfied so it does not have external dependencies, it now depends on Nz and addLog.</p>\n\n<p>Anyway, the problem here seems to be that you are looping from 0 to len()-1. In VBA this would be 1 to n.</p>\n\n<pre><code> Dim cell_val As String\n cell_val = "øabcdæøå~!#%&amp;/()"\n Dim iter As Long\n For iter = 1 To Len(cell_val)\n    If Asc(Mid(cell_val, iter, 1)) &gt; 127 Then\n       \'addlog "Export contains ascii character &gt; 127"\n       Debug.Print iter, "Export contains ascii character &gt; 127"\n    End If\n Next iter\n</code></pre>\n'}, {'answer_id': 80530, 'author': 'bfabry', 'author_id': 924607, 'author_profile': 'https://Stackoverflow.com/users/924607', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>Did you debug it? ;) Are you sure the\n  cell_val is not empty? Also you don't\n  need the 'Step 1' in the For loop\n  since it's default. Also what do you\n  expect to acomplish with your code? It\n  logs if any ascii values are above\n  127? But that's it - there is no\n  branching depending on the result?</p>\n</blockquote>\n\n<p>I didn't debug it, I have no idea how to use vba or any of the tools that go along with it.\nYes I am sure cell_val is not empty.\nThe code was representative, I was ensuring the branch condition works before writing the branch itself.</p>\n\n<blockquote>\n  <p>I believe your problem is that in VBA string indexes start at 1 and not at 0.</p>\n</blockquote>\n\n<p>Ah, the exact kind of thing that goes along with vba that I was bound to miss, thank you.</p>\n"}, {'answer_id': 80531, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 2, 'selected': False, 'text': '<p>With VBA, VB6 you can just declare a byte array and assign a string value to it and it will be converted for you. Then you can just iterate through it like a regular array.</p>\n\n<p>e.g.</p>\n\n<pre><code>Dim b() as byte\nDim iter As Long\nb = CStr(Nz(fld.value, ""))\n\nFor iter = 0 To UBound(b)\n    if b(iter) &gt; 127 then\n        addlog "Export contains ascii character &gt; 127"\n    end if\nnext\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/80427', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/924607/']