我一直在尝试这个,但得到了相同的输出。我想在一个字符串数组中创建一个循环,但代码只访问第一个值。这是我试过的代码:
name = Environ("username")
user1= Array("name.name1", "name.name2")
user2= Array("name1.name1", "name1.name2")
For i = 0 To Size ' I used Size coz I will add more names
If name = user1(i) Then
MsgBox "User1"
ElseIf name = user2(i) Then
MsgBox "User2"
Else
errorLog
End If
Next
提前感谢您的帮助!
答案 0 :(得分:1)
除非您为Size
提供了明确的声明和起始值,否则当它在for
循环中使用时,它将采用值为0的变量数字类型。
所以你得到For i = 0 To 0
:即循环将迭代一次。
在模块顶部编写Option Explicit
有助于减少此类错误。
如果您想获得数组的大小,请使用LBound
和UBound
。
答案 1 :(得分:0)
这就是我为解决问题所做的工作。
name = Environ("username")
Dim found as Boolean
found = false
user1= Array("name.name1", "name.name2")
user2= Array("name1.name1", "name1.name2")
'to check for user2
For i = 0 To UBound(user1)
If name = user1(i) Then
MsgBox "User1"
found = true
End If
Next
'to check for user2
For i = 0 To UBound(user2)
If name = user2(i) Then
MsgBox "User2"
found = true
End If
Next
'Else if the username is not on the list, I have added a Boolean found.
if found = false then
MsgBox "Unidentified Access!"
end If
非常感谢你的所有指导!