简体   繁体   English

VBA Excel数组错误

[英]VBA Excel array error

I have to generate an ID using a macro and I'm facing some problems when there are 8 numeric values required. 我必须使用宏来生成ID,并且当需要8个数字值时,我遇到了一些问题。 The ID has to include numeric values for the 1st-8th charactera and the 9th must be alphabetic. ID必须包含第1至第8个字符的数字值,第9个必须为字母。 From the 10th character onwards it has to be spaces. 从第十个字符开始,它必须是空格。 These are my codes and I'm certain that there are no issues with the formula 这些是我的代码,我确定公式没有问题

Function GenerateRB()

strLastCharSelections = "X,W,M,L,K,J,E,D,C,B,A"

intNumber = GenerateRandomNumber(1000000, 9999999)

ReDim a(8)
For i = 1 To 8
a(i) = Mid(intNumber, i, 1)
Next

intTotal = a(1) * 9 + a(2) * 8 + a(3) * 7 + a(4) * 6 + a(5) * 5 + a(6) * 4 + a(7) * 3 + a(8) * 2


intRemainder = intTotal Mod 11

arrstrSplitLastCharSelections = Split(strLastCharSelections, ",")
strLastChar = arrstrSplitLastCharSelections(intRemainder)
GenerateRB = intNumber & strLastChar
End Function

The code works when its 该代码在其

    ReDim a(7)
For i = 1 To 7
a(i) = Mid(intNumber, i, 1)
Next

intTotal = a(1) * 9 + a(2) * 8 + a(3) * 7 + a(4) * 6 + a(5) * 5 + a(6) * 4 + a(7) * 3

Any help will be appreciated as I'm very new to this, Thank you! 任何帮助将不胜感激,因为我对此很陌生,谢谢!

I'm assuming that GenerateRandomnNumber will return a numeric in the specified range - in this case, a seven digit number between 1000000 and 9999999. 我假设GenerateRandomnNumber将返回指定范围内的数字-在这种情况下,将是介于1000000和9999999之间的7位数字。

So when selecting the i th digit in the statement a(i) = Mid(intNumber, i, 1) , there are no issues when i is 1 to 7 - however, when it's 8 - there is no eighth digit, so the code will fail. 因此,当在语句a(i) = Mid(intNumber, i, 1)选择第i个数字时,当i为1到7时没有问题-但是,当它为8时-没有第八位数字,因此代码将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM