excel中利用VBA生成一列随机字符串 代码
时间:2023-07-08 09:59:19
用下面的VBA代码可以在A1:A100区域中生成由大小写字母和数字组成的12位长度的随机字符串,从中挑选几个作为密码用还是不错的。
在Excel中按Alt+F11打开VBA编辑器,双击“工程”窗口中的某个工作表名称,在右侧的代码窗口中输入下列代码:
Sub MakeRandomString()
Dim J As Integer
Dim K As Integer
Dim iTemp As Integer
Dim sNumber As String
Dim RandomStr(1 To 100, 1 To 1) As String
Dim bOK As Boolean
Randomize
For J = 1 To 100
sNumber = ""
For K = 1 To 12
Do
iTemp = Int((122 – 48 + 1) * Rnd + 48)
Select Case iTemp
Case 48 To 57, 65 To 90, 97 To 122
bOK = True
Case Else
bOK = False
End Select
Loop Until bOK
bOK = False
sNumber = sNumber & Chr(iTemp)
Next K
RandomStr(J, 1) = sNumber
Next J
Range("A1:A100").Value = RandomStr
End Sub
最后运行代码。如果要生成其他长度的随机字符串,将其中的
For K = 1 To 12
中的“12”改为其他数字即可,如:
For K = 1 To 10
即可生成10位长度的随机字符串。
标签:代码,字符串,生成,随机,Excel函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
edge浏览器看视频卡顿_edge浏览器看视频卡顿解决方法
2023-09-18 08:56:18
![](https://img.aspxhome.com/file/2023/3/a298391_0s.jpg)
Win10开机一直进不了系统怎么重装
2023-08-21 15:04:22
![](https://img.aspxhome.com/file/2023/25/a217815_0s.png)
iA Writer文字编辑器,心无旁骛,全神贯注写作!
2023-01-24 17:21:04
![](https://img.aspxhome.com/file/2023/8/a209658_0s.jpeg)
LEFT、LEFTB 函数
2022-09-13 13:24:52
电脑蓝屏综合解决方案
2023-12-31 06:36:49
![](https://img.aspxhome.com/file/2023/4/a306675_0s.png)
Word域代码特殊指令创建分数和积分的详细教程
2023-07-15 23:10:32
![](https://img.aspxhome.com/file/2023/1/25991_0s.jpg)
workflow.exe是什么进程
2022-08-16 12:08:50
excel怎样插入页码
2023-03-28 22:43:30
设置excel 2010数据有效性序列图文教程
2022-08-23 16:49:03
![](https://img.aspxhome.com/file/2023/4/42804_0s.jpg)
Win10快速截图技巧汇总
2023-01-16 14:05:09
![](https://img.aspxhome.com/file/2023/26/a227616_0s.jpg)
qq安全中心如何关闭人脸识别?qq安全中心关闭人脸识别的方法
2022-09-26 14:55:10
![](https://img.aspxhome.com/file/2023/8/a333285_0s.jpg)
使用LARGE函数为指定范围内的数值分类
2023-05-16 22:42:47
使用WPS保护教学信息安全
2023-06-16 09:46:37
![](https://img.aspxhome.com/file/2023/4/a165064_0s.jpg)
CDR实例教程:如何使用CorelDRAW 2019绘制谷歌浏览器Logo
2022-09-28 21:26:30
![](https://img.aspxhome.com/file/2023/0/a211730_0s.jpeg)
Win10如何更改鼠标样式?Win10更改鼠标样式教程
2022-11-25 10:16:58
![](https://img.aspxhome.com/file/2023/1/a283286_0s.jpg)
excel中函数求最高值的操作方法
2022-12-26 17:05:09
让Win10系统按F8进Win7安全模式一个命令菜单
2022-03-07 11:28:28
![](https://img.aspxhome.com/file/2023/4/a313004_0s.png)
主板陷入万劫不复:今年再跌10%
2023-08-13 01:56:21
![](https://img.aspxhome.com/file/2023/4/a309447_0s.png)
在网上写信时 如何使用WPS制作一个信筏
2022-03-27 21:16:46
![](https://img.aspxhome.com/file/2023/4/a169744_0s.jpg)
Win10使用微软拼音输入特殊字符的具体操作方法
2023-09-06 01:25:25
![](https://img.aspxhome.com/file/2023/26/a224592_0s.jpg)