使用 Excel 中的公式等函数
我正在尝试使用公式来获取字母表中的字母。
公式:
=Keytable(RANDOM,ROW())
函数:
Function KeyTable(seed As Long, position As Long) As String
Dim i As Long
Stop
Dim calpha(1 To 26) As String
Dim alpha(1 To 26) As String
For i = 1 To 26
alpha(i) = Chr(i + UPPER_CASE - 1)
Next i
For i = 1 To 26
calpha(i) = alpha(seed Mod 27 - i)
Next i
Stop
KeyTable = calpha(position)
End Function
结果:
#Value!
当我单步执行该函数时,它永远不会到达第二个停止点。怎么了?
I am trying to use a formula to get a letter of the alphabet.
Formula:
=Keytable(RANDOM,ROW())
Function:
Function KeyTable(seed As Long, position As Long) As String
Dim i As Long
Stop
Dim calpha(1 To 26) As String
Dim alpha(1 To 26) As String
For i = 1 To 26
alpha(i) = Chr(i + UPPER_CASE - 1)
Next i
For i = 1 To 26
calpha(i) = alpha(seed Mod 27 - i)
Next i
Stop
KeyTable = calpha(position)
End Function
Result:
#Value!
When I step through the function, it never gets to the second stop. What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RANDOM
不是 Excel 中的函数。RAND()
是,它返回 0 到 1 之间的float
。您需要一个integer
来进行模数计算。要获取随机整数,请使用:
然后,一旦
seed Mod 27 - i
变为 0 或更小,该函数就会终止,因为在 VBA(或大多数语言)中无法使用 0 或更小的索引对数组进行索引。但实际上,对于随机字母,您所需要做的就是:
RANDOM
is not a function in Excel.RAND()
is and it returns afloat
between 0 and 1. You need aninteger
to do modulus calculations.To get a random integer, use:
Then, once
seed Mod 27 - i
becomes 0 or less, the function dies because arrays can't be indexed with 0 or less in VBA (or most languages).But really all you need to do for a random letter is this:
此代码将返回随机字母:
This code will return random letter of alphabet: