跳到主要内容

如何在Excel中生成一个随机字符串?

有时您可能需要在单元格中生成随机字符串,例如不同的密码。 本文试图向您展示一些技巧,以便在Excel中生成不同的随机字符串。

使用公式生成随机字符串
使用VBA代码生成随机字符串
使用 Kutools for Excel 轻松生成随机字符串


使用公式生成随机字符串

以下公式可以帮助您在Excel的范围内生成随机数,字母和字母数字值。

1。 要创建介于5和10000之间的99999位随机数,请使用以下公式: = RANDBETWEEN(10000,99999),然后按 输入 键,一个5位数字将显示在一个单元格中,然后选择该单元格并将填充手柄拖到要包含此公式的范围内,已生成一系列5位数字,请参见屏幕截图:

文档生成字符串1 -2 文档生成字符串2

备注:您可以更改参数以获取需要。

2。 要创建随机字母,请使用以下公式: = CHAR(RANDBETWEEN(65,90))。 此公式可以生成一个从a到z的随机字母,如果需要创建四个随机字母,则需要使用&字符添加字母。 如 = CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))。 然后,您将获得四个随机字母,然后将填充手柄拖到要包含此公式的范围内。 看截图:

文档生成字符串3

:

(1.)该公式在Excel 2007、2010和2013中有效,但在Excel 2003中无效。在Excel 2003中,请使用该公式 = CHAR(INT(RAND()* 25 + 65))&CHAR(INT(RAND()* 25 + 65))&CHAR(INT(RAND()* 25 + 65))&CHAR(INT(RAND() * 25 + 65))

(2.)在公式65中是A而90是Z。

(3.)您可以使用&字符添加所需字母的数量。

3。 若要创建具有两个字母和两个数字的随机字母数字字符串,请使用以下公式: = CHAR(RANDBETWEEN(65,90))&CHAR(RANDBETWEEN(65,90))&RANDBETWEEN(10,99) 并且您将在包含两个字母和两个数字的范围内获得以下字符串:

文档生成字符串4

备注:您可以更改参数以根据需要获取数字,并使用&添加字母数。

轻松生成选定范围内的随机字符串:

生成带有特定字符和文本长度的随机字符串很容易, 插入随机数据 实用程序 Kutools for Excel.
立即下载 Excel 版 Kutools! (30 天免费试用)


使用VBA代码生成随机字符串

如果您认为上述公式很困难且很麻烦,那么下面的VBA代码可以帮助您轻松得多。 请这样做:

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下宏粘贴到 模块 窗口。

Public Function RandomizeF(Num1 As Integer, Num2 As Integer)
'Update 20131107
Dim Rand As String
Application.Volatile
getLen = Int((Num2 + 1 - Num1) * Rnd + Num1)
Do
    i = i + 1
    Randomize
    Rand = Rand & Chr(Int((85) * Rnd + 38))
Loop Until i = getLen
RandomizeF = Rand
End Function

3。 然后保存并关闭代码,在单元格中输入此功能 = RandomizeF(x,y) 插入随机字符串,其最小长度为x个字符,最大长度为y个字符。

4。 在这个例子中,我将使用功能 = RandomizeF(5,10) 生成一个5到10个字符的字符串。 然后按 输入 键,选择单元格并将填充手柄拖动到要包含此功能的范围。 并随机创建了5到10个字符的字母数字和特定字符串。 看截图:

文档生成字符串5


使用 Kutools for Excel 生成随机字符串

有没有一种方法可以生成带有字母,数字和特殊字符或更多字符的随机字符串? Kutools for Excel's 插入随机数据 是一个出色的随机数(和文本字符串)生成器,可以生成具有各种字符的随机数,随机文本字符串或随机数,文本和符号,包括字母,数字,特殊字符,空格,甚至是自定义字符串。

申请前 Kutools for Excel首先下载并安装.

1。 选择一个将在其中生成随机字符串的范围,然后单击 库工具 > 插页 > 插入随机数据。 看截图:

3。 在 插入随机数据 对话框,单击 标签,然后根据需要选择字符类型,然后在 弦长 框,最后单击 OK 按钮。 看截图:

然后,所选范围已用随机字符串填充。

Tips:注意:如果要在范围内生成或插入指定的格式数据字符串(例如???? @。??。com),则也可以使用此实用程序。 这样做:

1。 选择范围并指定字符并检查 面膜。 然后输入所需的指定数据字符串。 看截图:

备注:使用 ? 在最终指定的格式字符串中指示一位随机字符。

2。 然后点击 OK or 使用。 指定的格式数据字符串已按以下方式随机生成。 看截图:

Kutools for Excel's 插入随机数据 可以帮助您轻松地在范围单元格中处理以下操作。

  • 生成或插入范围内的随机数
  • 生成或插入范围内的随机日期
  • 生成或在范围内插入自定义列表

  如果您想免费试用(30天)此实用程序, 请点击下载,然后按照上述步骤进行操作。


演示:生成范围内的随机字符串


相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!

Comments (29)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I found this useful as a starting point--I hadn't touched VBA in several years, so it was way easier to use the sample code here as a basis than start from scratch. Skelly1008, have you thought about something like this? Do Randomize charVal = (Int(85 * Rnd) + 48) If charVal > &H30 And charVal < &H7A Then If Not (charVal > &H5A And charVal < &H61) Then If Not (charVal > &H39 And charVal < &H41) Then newChar = Chr(charVal) Rand = Rand & newChar End If i = i + 1 End If End If Loop Until i = getLen That generates strings that contain only a-z, A-Z, and 0-9.
This comment was minimized by the moderator on the site
I'm looking for a code that can generate any number 0 to 9 and or any letter A to Z. But I need 25 characters in the that final out put.
This comment was minimized by the moderator on the site
Found way: 1st: For each char: =RANDBETWEEN(0;1) to randomly select a number or a letter (result in A2 to A9, for example) 2nd: =IF(An=0;RANDBETWEEN(0;9);CHAR(RAND()*26+97)) - in B2 to B9 -> to generate a number or a letter depending on result in A column 3rd: in the cell you want the generated password: =B2&B3&B4&B5&B6&B7&B8&B9
This comment was minimized by the moderator on the site
I live the formula but once I enter something in another field the numbers in the random fields change. I only want it to randomly generate one time. Not every time I enter data in other fields on the worksheet. Is that a separate function? Thanks!
This comment was minimized by the moderator on the site
Hi I want one help from you. I have to replace one last three values in this text for example: LoadTesting . I want to change only last three word of it
This comment was minimized by the moderator on the site
[quote]Hi I want one help from you. I have to replace one last three values in this text for example: LoadTesting . I want to change only last three word of itBy rOHIT[/quote] Assuming it is in cell A1:

=MID(A1,1,LEN(A1)-3)&"CAT"
This comment was minimized by the moderator on the site
Hi, On generating the random number in excel,can we insert the value to a form?any query plz share
This comment was minimized by the moderator on the site
This is several times better than I dared hope for!! I completely disregarded the idea of generating several values in a single cell. Guaranteed I'll be using this info every chance I get! 11/10.
This comment was minimized by the moderator on the site
Hi i would like to make the same in a MS WORD tab. is it possible ?
This comment was minimized by the moderator on the site
The VBA isn't working as a true random number. I'm using (10,12) as the criteria and if I run it down a couple thousand rows and do a countif formula in the adjacent column I find many duplicate passwords.
This comment was minimized by the moderator on the site
Very helpful, thank you. Using the VBA code, is it possible to only show letters and numbers and not other symbols?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations