我需要一些帮助来创建一个3个字母的随机名称生成器。
例如K,H,F。
每个名称必须包含三个“K”,两个“H”和一个“F”。
有谁知道?
非常感谢你的帮助!
答案 0 :(得分:0)
以下是您可以放入模块的功能:
Public Function Random(RLength As Integer) As String
On Error GoTo Err_Random
Dim strTemp As String
Dim intLoop As Integer
Dim strCharBase As String
Dim intPos As Integer
Dim intLen As Integer
Startover:
Random = ""
strCharBase = "KHF"
intLen = Len(strCharBase)
strTemp = String(RLength, "A")
Rnd -1
Randomize (Timer)
For intLoop = 1 To Len(strTemp)
intPos = CInt(Rnd() * intLen + 1)
If intPos > intLen Then intPos = intPos - 1
Mid$(strTemp, intLoop, 1) = Mid$(strCharBase, intPos, 1)
Next
Random = strTemp
If (Len(Random) - Len(Replace(Random, "K", ""))) / Len("K") <> 3 Then GoTo Startover
If (Len(Random) - Len(Replace(Random, "H", ""))) / Len("H") <> 2 Then GoTo Startover
If (Len(Random) - Len(Replace(Random, "F", ""))) / Len("F") <> 1 Then GoTo Startover
Exit_Random:
On Error Resume Next
Exit Function
Err_Random:
MsgBox Err.Number & " " & Err.Description, vbCritical, "Random"
Random = ""
Resume Exit_Random
End Function
在表单上调用函数,如:
=Random(6)
可以找到此代码的更好细分here
显然这适用于六个字母的字符串。但是,如果增加字符串的长度,则必须为strCharBase分配更多字符。如果你这样做,它会得到一个更长的过程。
答案 1 :(得分:0)
这是一种愚蠢的方式:
在范围A1:A6
中输入公式=RAND()
并将您的信件放在B1:B6
中。
在另一个单元格中输入公式:
=CONCATENATE(VLOOKUP(SMALL(A1:A6,1),A1:B6,2,FALSE),VLOOKUP(SMALL(A1:A6,2),A1:B6,2,FALSE),VLOOKUP(SMALL(A1:A6,3),A1:B6,2,FALSE),VLOOKUP(SMALL(A1:A6,4),A1:B6,2,FALSE),VLOOKUP(SMALL(A1:A6,5),A1:B6,2,FALSE),VLOOKUP(SMALL(A1:A6,6),A1:B6,2,FALSE))
像这样工作: