博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VBA使用记录
阅读量:2430 次
发布时间:2019-05-10

本文共 2551 字,大约阅读时间需要 8 分钟。

没怎么用过VBA,突然被要求用VBA实现一个简单的抽奖算法,来来回回也花了不少时间,把代码记录下来,为了以后方便查找。

数据源:第一个sheet输入指定数据,含有执行按钮。第二个sheet有3列,第一列是手机号,第二列是手机被使用次数,第三列是手机最早一次被使用的时间

实现1:在手机号中找出与指定数据含有相同数字最多的手机号,如果有多个,找出使用次数最多的,还有多个再看使用时间最早的那个。

Sub luckyPrise()Dim phoneIndex, phoneNumMatchTotal, maxMatch As IntegerDim candidateCol As CollectionDim stockNumMatchDict 'As DictionarySet candidateCol = New CollectionSet stockNumMatchDict = CreateObject("Scripting.Dictionary")Dim stockNumMatch, phoneNumMatch As IntegerFor Num = 0 To 9  stockNumMatch = Len(Sheets("sheet1").Cells(1, 1)) - Len(WorksheetFunction.Substitute(Sheets("sheet1").Cells(1, 1), Num, ""))  stockNumMatchDict.Add Num, stockNumMatchNext Num' debug'For i = 0 To stockNumMatchDict.Count - 1 '重复数组'    MsgBox stockNumMatchDict.Item(i)          '打印条目'Next    phoneIndex = 0maxMatch = 0Do While Sheets("sheet2").Cells(phoneIndex, 1) <> ""  phoneNumMatchTotal = 0  For Num = 0 To 9     phoneNumMatch = Len(Sheets("sheet2").Cells(phoneIndex, 1)) - Len(WorksheetFunction.Substitute(Sheets("sheet2").Cells(phoneIndex, 1), Num, ""))     If phoneNumMatch > stockNumMatchDict.Item(Num) Then         phoneNumMatch = stockNumMatchDict.Item(Num)     End If     phoneNumMatchTotal = phoneNumMatchTotal + phoneNumMatch  Next Num    Sheets("sheet2").Cells(phoneIndex, 5) = phoneNumMatchTotal    If maxMatch < phoneNumMatchTotal Then    maxMatch = phoneNumMatchTotal  End If    phoneIndex = phoneIndex + 1  LoopEnd Sub

实现2:找出与指定数据最后2位一致的手机号码,如果有多个,和实现1相同规则找出唯一一个。

Sub Prise()    Dim phoneNumStr As String    Dim stockLastTwoNumStr As String    Dim phoneNumRowIndex As Integer        stockLastTwoNumStr = ActiveWorkbook.Sheets("Sheet1").Cells(1, 2)    'sort the data with forward numbers descending and first forward data ascending    ActiveWorkbook.Sheets("Sheet2").Columns("A:C").Sort key1:=ActiveWorkbook.Sheets("Sheet2").Range("B2"), _        order1:=xlDescending, Header:=xlYes, key2:=ActiveWorkbook.Sheets("Sheet2").Range("C2"), _        order1:=xlAscending, Header:=xlYes         phoneNumRowIndex = 2        phoneNumStr = ActiveWorkbook.Sheets("Sheet2").Cells(phoneNumRowIndex, 1)        Do While phoneNumStr <> ""        If InStr(10, phoneNumStr, stockLastTwoNumStr) = 10 Then            MsgBox "获奖手机号: " & ActiveWorkbook.Sheets("Sheet2").Cells(phoneNumRowIndex, 1)            Exit Sub        End If                phoneNumRowIndex = phoneNumRowIndex + 1        phoneNumStr = ActiveWorkbook.Sheets("Sheet2").Cells(phoneNumRowIndex, 1)    Loop        MsgBox "没有匹配手机号,选择转发次数最多手机中最早转发的:" & ActiveWorkbook.Sheets("Sheet2").Cells(2, 1)    End Sub

转载地址:http://xbtmb.baihongyu.com/

你可能感兴趣的文章
程序员爬取 3 万条评论,《长安十二时辰》槽点大揭秘!
查看>>
一年参加一次就够,全新升级的 AI 开发者大会议程出炉!
查看>>
基于 XDanmuku 的 Android 性能优化实战
查看>>
基于嵌入式操作系统的物联网安全
查看>>
一个只有 99 行代码的 JS 流程框架
查看>>
移动周刊第 186 期:移动 App 客户端性能优化、iOS 开源库源码解析
查看>>
包学会之浅入浅出 Vue.js:开学篇
查看>>
JavaScriptCore 全面解析 (上篇)
查看>>
移动周刊第 187 期:App 模块化实战经验总结
查看>>
以不一样的视角看物联网协议
查看>>
JavaScriptCore全面解析 (下篇)
查看>>
嵌入式操作系统与物联网演进之路
查看>>
苹果公司揭秘首批列入 Swift 源代码兼容性开源项目清单
查看>>
Python 玩转物联网之 Micropython GPIO IRQ 处理
查看>>
移动周刊第 188 期:Android 安全性要点与规范核心详析
查看>>
手机为基础的 IoT 布局已经失效,下一代操作系统是什么模样?
查看>>
无线传感器网络使用指南
查看>>
《近匠》专访机智云 CTO 刘琰——从 0 到 1 开启智能化硬件开发
查看>>
深度对话微软,解读 HoloLens 技术设计细节
查看>>
移动周刊第 191 期:如何看待 Kotlin 成为 Android 官方支持开发语言?
查看>>