Excel自定义函数求助
程序代码:
使用示例1(帖子要求的长度26的超级单词):
使用示例2(随机长度的随机单词):
程序代码文本(有可能特殊符号被浏览器偷吃,以上图代码为准):
Option Explicit
Function 随机不重复字母串(Optional ByVal n& = 26) As String
Dim a(1 To 26), i&, j&, s$
'初始化
For i = 1 To 26
a(i) = Chr(64 + i)
Next i
'打乱
For i = 1 To 26
j = Int(Rnd() * 26 + 1)
s = a(j)
a(j) = a(1)
a(1) = s
Next i
'随机小写
For i = 1 To 26
j = Int(Rnd() * 26 + 1)
If a(j) < "a" Then a(j) = Chr(Asc(a(j)) + 32)
Next i
'合成和返回
随机不重复字母串 = Left(Join(a, ""), n)
End Function
=CHAR(SMALL(IF(COUNTIF($A1:A1,CHAR(ROW(65:90)))=0,ROW(65:90)),RANDBETWEEN(1,27-COLUMN(A:A)))+IF(RAND()>0.5,32,))
同时按下CTRL+SHIFT+ENTER,输入数组公式,再用自动填充柄将B1公式右拉到AA1
可多次按下F9,查看效果。
三键结束
代码视图:
计算结果:
代码如下:
'复制到模块
Function Exchange()
Dim Arr1(25) As String, i%, j, K, Rndrange As Long, Rnd1 As Double
'生成26个英文大写字母
For i = 0 To 25
Arr1(i) = Chr(65 + i)
Next i
Rndrange = UBound(Arr1)
'交换数组元素位置
For j = 0 To 25
Randomize (Timer)
Rnd1 = Int(Rndrange * Rnd)
K = LCase(Arr1(j))
Arr1(j) = Arr1(Rnd1)
Arr1(Rnd1) = K
Next j
Exchange = Join(Arr1) '不重复的数列
End Function