【代码】数据脱敏:随机生成手机号码;根据“姓”、“名”列表随机生成姓名

文摘   教育   2024-09-15 23:10   江苏  

点【关于本公众号】了解一下,欢迎关注谢谢!

快速浏览

实用案例

|日期控件||简单的收发存||收费管理系(Access改进版)|

|电子发票管理助手||电子发票登记系统(Access版)|

|文件合并||表格拆分||审计凭证抽查底稿|

|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|

|印章使用登记系统|

收费使用项目

|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|

内容提要

  • 随机生成姓名手机号码|完整代码
1、在工作表“学生名单”里,命令按钮点击事件,调用生成数据过程:
Private Sub CmdGenerateData_Click()    Call generateDataEnd Sub
2、在myModule模块里,generateData过程,生成随机姓名、手机号,存入学生名单表
Sub generateData()    Dim arr(), dic As Object, arrName(), arrNum(), rng As Range    Dim lastRow As Long, lastCol As Long    Dim ws As Worksheet, num As Long    Set ws = ThisWorkbook.Sheets("学生名单")    Set dic = CreateObject("Scripting.Dictionary")    arr = ws.Range("A1").CurrentRegion    num = UBound(arr) - 1    For i = 1 To num        dic(arr(i + 1, 3)) = 1        dic(arr(i + 1, 5)) = 1    Next    arrName = generateNames(1, num, dic)    arrNum = generatePhoneNums(num, dic)    With ws        .Cells(2, 3).Resize(UBound(arrName)) = Application.Transpose(arrName)        Set rng = .Cells(2, 5).Resize(UBound(arrNum))        With rng            .NumberFormat = "@"            .Value = Application.Transpose(arrNum)        End With    End WithEnd Sub
3、在myModule模块里,自定义函数generateNames,生成姓名;自定义函数generatePhoneNums,生成随机手机号码
Function generateNames(gType As Integer, num As Long, exDic As Object) As Variant    Dim arr(), arrFirstName(), arrLastName()    Dim lastName As String, firstName As String, currName As String, rndName As String    Dim ws As Worksheet    Dim dic As Object    Dim IsValidName As Boolean    Dim lastRow As Long, rndRow As Long    Dim i As Long        Set ws = ThisWorkbook.Sheets("姓名")    Set dic = CreateObject("Scripting.Dictionary")    ReDim arr(1 To num)        '// 读取姓氏和名字列表    With ws        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row        arrLastName = .Cells(2, 1).Resize(lastRow - 1, 1).Value        lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row        arrFirstName = .Cells(2, 2).Resize(lastRow - 1, 1).Value    End With        For i = 1 To num        IsValidName = False        Do While IsValidName = False            '// 随机选择姓氏            rndRow = Int((UBound(arrLastName) - LBound(arrLastName) + 1) * Rnd + LBound(arrLastName))            lastName = arrLastName(rndRow, 1)                        '// 随机选择名字            rndRow = Int((UBound(arrFirstName) - LBound(arrFirstName) + 1) * Rnd + LBound(arrFirstName))            firstName = arrFirstName(rndRow, 1)            currName = lastName & firstName                        '// 根据 gType 确定姓名长度            If gType = 3 Then                '// 生成三个字的姓名                rndRow = Int((UBound(arrFirstName) - LBound(arrFirstName) + 1) * Rnd + LBound(arrFirstName))                rndName = arrFirstName(rndRow, 1)                currName = currName & rndName            ElseIf gType = 1 Then                '// 生成2~3个字的姓名                If Rnd < 0.5 Then                    rndRow = Int((UBound(arrFirstName) - LBound(arrFirstName) + 1) * Rnd + LBound(arrFirstName))                    rndName = arrFirstName(rndRow, 1)                    currName = currName & rndName                End If            End If                        '// 检查姓名是否已存在            If Not exDic.exists(currName) And Not dic.exists(currName) Then                arr(i) = currName                dic(currName) = 1                IsValidName = True            End If        Loop    Next        generateNames = arrEnd Function
Function generatePhoneNums(num As Long, exDic As Object) As Variant Dim arr(), arr2() Dim ws As Worksheet Dim dic As Object Dim IsValidNum As Boolean Dim currNum As String, rndNum As Long, rndRow As Long Dim i As Long Set ws = ThisWorkbook.Sheets("姓名") Set dic = CreateObject("Scripting.Dictionary") ReDim arr(1 To num) arr2 = Array(3, 5, 7, 8, 9) For i = 1 To num IsValidNum = False Do While IsValidNum = False rndRow = Int((UBound(arr2) - LBound(arr2) + 1) * Rnd + LBound(arr2)) rndNum = Int(999999999 * Rnd) currNum = "1" & arr2(rndRow) & Format(rndNum, "000000000") '// 检查号码是否存在于 exDicdic If Not exDic.exists(currNum) And Not dic.exists(currNum) Then arr(i) = currNum dic(currNum) = 1 IsValidNum = True End If Loop Next generatePhoneNums = arrEnd Function

~~~~~~End~~~~~~

安利小店
安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精洗衣液也是日常必备,用过都说好!

合谷医疗
合谷医疗专攻各种疑难杂症,尤其擅长抑郁症焦虑失眠儿童神经发育异常多动症自闭孤独症腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了

喜欢就点个、点在看留言评论、分享一下呗!感谢支持!

案例文件分享说明

  • 案例文件可免费分享,但需符合以下要求:

  • 关注点赞点在看点...留言,方便的话分享一下就完美啦!如果不便走上面的“流程”,请打赏,万分感谢!

  • 请添加上方我的合谷医疗企业微信,案例文件通过微信发送。如有定制需求,亦可通过微信联系。

  • Excel问题,请在文章下面留言讨论!或者加入我的付费交流群提问

VBA编程实战
Excel应用案例、Excel VBA、公式函数使用技巧分享,思路解读...... 这里有鲜活案例、实用的技巧......
 最新文章