ExcelVBA生成不重复n位的n个数字

2022-10-25 13:38:56 浏览数 (1)

Sub test()

Call rnd_n_n(18, 100, "a1")

End Sub

'************************************

'为了在做表的时候模拟出一些身份证,本程序可以生成100个不重复的18位数字

'要求:1.18位数字 2.同时生成的总量中没有重复

'使用方法:call rnd_n_n(位数,个数,存放位置)如:Call rnd_n_n(10, 100, "a1")

'*************************************************

Sub rnd_n_n(nw, ng, rng)

' Const a = "ABCDEFGHJKLMNOPQRSTWXYZZ0123456789"

Const a = "0123456789"

Dim i&, z As String

Set d = CreateObject("scripting.dictionary")

10:

For i = 1 To nw '18是要生成的位数。如果你要15位就修改为15

z = z & Mid(a, WorksheetFunction.RandBetween(1, 10), 1)

Next

If Not d.Exists(z) Then

d(z) = ""

If d.Count < ng Then '100是要生成的总数,如果是要500个就修改为500

z = "": GoTo 10

Else

GoTo 100

End If

Else

z = "": GoTo 10

End If

100:

Range(rng).Resize(d.Count, 1).NumberFormat = "@"

Range(rng).Resize(d.Count, 1) = WorksheetFunction.Transpose(d.keys)

End Sub

0 人点赞