excel - 如何使用 Randomize 使我的 UDF 成为非易失性?
问题描述
我正在尝试创建一个为我创建随机 ID 的函数。
在 Excel 中使用它时,ID 列将填充: =UPPER(CONCATENATE(LEFT([@Group],4),RandomString(4)))
除非 Random String 中的长度输入 (4) 发生变化,否则我不希望公式的 Randomise 部分再次重新计算(我永远不会更改长度,因此它将是静态的)。唯一应该更改的字符是与 (LEFT([@Group],4).
现在,当我更改组列中的任何单元格时,我的 TaskID 列中的所有单元格都会重新计算,因此应用程序 volatile 并没有足够的帮助。
谢谢
Function RandomString(Length As Integer)
Application.Volatile False
Dim CharacterBank As Variant
Dim x As Long
Dim str As String
'Test Length Input
If Length < 1 Then
MsgBox "Length variable must be greater than 0"
Exit Function
End If
CharacterBank = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", _
"W", "X", "Y", "Z")
'Randomly Select Characters One-by-One
For x = 1 To Length
Randomize
str = str & CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))
Next x
'Output Randomly Generated String
RandomString = str
End Function
解决方案
推荐阅读
- javascript - 如何从另一个浏览器访问浏览器 JavaScript API
- ssl - 无法在 python2.6 中使用 PIP 更新/添加任何包
- dynamics-crm - 汇总字段十进制抛出错误
- pdf - 是否可以将fabricjs svg输出转换为pdf而不进行光栅化?
- python - PyCharm 在未选中检查时会出现代码兼容性错误
- swift - 设置语言环境时UIDatePicker错误的时间位置
- r - 根据共享值合并两个数据框
- ios - UIImagePickerController 显示带有灰色背景的专辑列表行
- json - 如何修复 json 中的换行错误:非法未引用字符 ... 必须使用反斜杠进行转义
- mongodb - 引用集合的归档而不是集合 mongodb