vba 如何遍历字符串并检查每个字符的字节值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/80427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate through a string and check the byte value of every character?
提问by bfabry
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.
此代码不起作用。有人知道怎么做吗?我对 VB 或 VBA 一无所知。
回答by jan.vdbergh
I believe your problem is that in VBA string indexes start at 1 and not at 0. Try the following:
我相信你的问题是在 VBA 字符串索引从 1 开始而不是从 0 开始。尝试以下操作:
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
回答by Sam
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.
使用 VBA、VB6,您只需声明一个字节数组并为其分配一个字符串值,它将为您进行转换。然后你可以像普通数组一样遍历它。
e.g.
例如
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
回答by vzczc
Your example should be modfied so it does not have external dependencies, it now depends on Nz and addLog.
你的例子应该被修改,所以它没有外部依赖,它现在依赖于 Nz 和 addLog。
Anyway, the problem here seems to be that you are looping from 0 to len()-1. In VBA this would be 1 to n.
无论如何,这里的问题似乎是您从 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
回答by bfabry
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?
你调试了吗?;) 你确定 cell_val 不是空的吗?此外,您不需要 For 循环中的“第 1 步”,因为它是默认设置。另外你希望用你的代码完成什么?它记录是否有任何 ascii 值高于 127?但就是这样 - 根据结果没有分支?
I didn't debug it, I have no idea how to use vba or any of the tools that go along with it. Yes I am sure cell_val is not empty. The code was representative, I was ensuring the branch condition works before writing the branch itself.
我没有调试它,我不知道如何使用 vba 或任何与之配套的工具。是的,我确定 cell_val 不为空。代码具有代表性,我在编写分支本身之前确保分支条件有效。
I believe your problem is that in VBA string indexes start at 1 and not at 0.
我相信你的问题是在 VBA 字符串索引从 1 开始而不是从 0 开始。
Ah, the exact kind of thing that goes along with vba that I was bound to miss, thank you.
啊,正是我一定会错过的与 vba 相关的东西,谢谢。
回答by Per Hornsh?j-Schierbeck
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?
你调试了吗?;) 你确定 cell_val 不是空的吗?此外,您不需要 For 循环中的“第 1 步”,因为它是默认设置。另外你希望用你的代码完成什么?它记录是否有任何 ascii 值高于 127?但就是这样 - 根据结果没有分支?
回答by Scott Evernden
Try AscW()
试试 AscW()
回答by paxdiablo
VB/VBA strings are based from one rather than zero so you need to use:
VB/VBA 字符串基于一而不是零,因此您需要使用:
For iter = 1 To Len(cell_val)
I've also left off the step 1since that's the default.
我也离开了,step 1因为这是默认设置。

