首页 > 代码库 > 20170624xlVBA正则分割分类汇总
20170624xlVBA正则分割分类汇总
Sub RegExpSubtotal() ‘声明变量 Dim Regex As Object ‘正则对象 Dim Dic As Object ‘字典对象 Dim Key As String ‘关键字 Dim Item As Double ‘项内容 Dim Index As Long ‘序号 Dim Text As String ‘文本 Dim Mch As Object ‘匹配集合 Dim OneMch As Object ‘匹配子项 Dim Rng As Range ‘单元格对象 ‘实例化 正则对象和字典对象 Set Regex = CreateObject("VBScript.RegExp") Set Dic = CreateObject("Scripting.Dictionary") With Regex .Global = True ‘匹配模式 浙江4000安徽19963.78 Pattern = "([^\d\.]+)([\d\.]+)" .Pattern = Pattern End With ‘逐行循环收款明细 For Index = 2 To Cells(Cells.Rows.Count, 1).End(xlUp).Row Text = Cells(Index, "A").Text ‘取得文本 Set Mch = Regex.Execute(Text) ‘执行匹配 For Each OneMch In Mch ‘循环匹配集合 Key = OneMch.SubMatches(0) ‘客户名称 Item = CDbl(OneMch.SubMatches(1)) ‘收款金额 Dic(Key) = Dic(Key) + Item ‘分类汇总 Next OneMch Next Index ‘快速转置输出分类汇总内容 Set Rng = Range("C1").Resize(Dic.Count, 2) Rng.Value = http://www.mamicode.com/Application.WorksheetFunction.Transpose(Array(Dic.Keys, Dic.Items))>
20170624xlVBA正则分割分类汇总
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。