vba学习系列(9)--按需求计数单位格数目

[复制链接]
发表于 2025-12-10 07:14:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
系列文章目次




前言

一、按需求计数单位格数目

1.需求

   一个表中有多个范例的单位格内容,好比:笔墨、数字、特殊字符、字母+数字……
我们要计数字母+数字的单位格数目
同时提取字母+数字单位格的数字部分,判断数字部分是否雷同,然后计数差别的单位格数目
  二、使用步调

1.vba源码

   代码如下(示例):
  1. Sub CountSpecificFormatStrings()
  2.     Dim rng As Range
  3.     Dim cell As Range
  4.     Dim count, countkey As Integer
  5.     Dim regex, regex1 As Object
  6.     Dim cellAddress As String
  7.     Dim numbers As String
  8.     Dim matches As Object
  9.     Dim match As Variant
  10.     Dim cellNumber As String
  11.     Dim cellValue As String
  12.     Dim myCollection As Collection
  13.     Set myCollection = New Collection
  14.     Dim value As Variant
  15.     Dim lastValue As Variant
  16.     Dim uniqueCount As Integer
  17.     Dim ws As Worksheet
  18.     Set ws = ActiveSheet
  19.     Dim cellValueCollection As Collection
  20.     Dim i, j, k As Integer
  21.     Dim valueToAdd As String
  22.     Dim key As Variant
  23.     Dim dict As Object
  24.     Dim myArray As Variant
  25.     Dim item, ele As Variant
  26.     Dim elements As Variant
  27.     Dim uniquekeys As New Collection
  28.     uniqueCount = 0
  29.     lastValue = ""
  30.     Set cellCollection = New Collection
  31.     Set dict = CreateObject("Scripting.Dictionary")
  32.    
  33.     ' 设置要检查的范围
  34.     Set rng = ws.Range("A3:DR200") ' 假设我们在Sheet1的A1:A10范围内查找
  35.     count = 0
  36.    
  37.     ' 创建正则表达式对象
  38.     Set regex = CreateObject("VBScript.RegExp")
  39.    
  40.     ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
  41.     ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
  42.     With regex
  43.         .Global = True
  44.         .IgnoreCase = True
  45.         .pattern = "[A-Za-z][0-9]+"
  46.     End With
  47.    
  48.         ' 创建正则表达式对象
  49.     Set regex1 = CreateObject("VBScript.RegExp")
  50.    
  51.     ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
  52.     ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
  53.     With regex1
  54.         .Global = True
  55.         .IgnoreCase = True
  56.         .pattern = "\d+"
  57.     End With
  58.     ' 遍历单元格
  59.     For Each cell In rng
  60.         If regex.Test(cell.value) Then ' 如果单元格匹配正则表达式
  61.             count = count + 1 ' 增加计数
  62.    
  63.             Debug.Print "行:" & cell.Row & ",列:" & cell.Column & ",值:" & cell.value
  64.             
  65.             'Set matches = regex1.Execute(cell.Value)
  66.             'For Each match In matches
  67.                 'outputString = outputString & match.Value
  68.                 'Debug.Print outputString
  69.             'Next match
  70.             'Debug.Print cell.Row & cell.Column
  71.             'Debug.Print Cells(cell.Row, cell.Column).Value
  72.             Set matches = regex1.Execute(Cells(cell.Row, cell.Column).value)
  73.             For Each match In matches
  74.                 myCollection.Add match
  75.             Next match
  76.             
  77.         End If
  78.     Next cell
  79.    
  80.    
  81.     For Each item In myCollection
  82.         'Debug.Print item
  83.     Next
  84.    
  85.     countkey = myCollection.count
  86.    
  87.     For i = myCollection.count To 1 Step -1
  88.         For j = 1 To i - 1
  89.             If myCollection(i) = myCollection(j) Then
  90.                 myCollection.Remove (i)
  91.                 countkey = countkey - 1
  92.                 Exit For
  93.             End If
  94.         Next j
  95.     Next i
  96.    
  97.     For Each ele In myCollection
  98.         Debug.Print ele
  99.     Next ele
  100.     ws.Cells(2, 69).value = countkey
  101.     ' 显示计数结果
  102.     MsgBox "有 " & countkey & " 个单元格符合指定格式。"
  103. End Sub
复制代码
2.整理后

   代码如下(示例):
  1. Sub CountSpecificFormatStrings()
  2.     Dim rng As Range
  3.     Dim cell As Range
  4.     Dim count, countkey As Integer
  5.     Dim regex As Object
  6.     Dim matches As Object
  7.     Dim match As Variant
  8.     Dim myCollection As Collection
  9.     Set myCollection = New Collection
  10.     Dim ws As Worksheet
  11.     Set ws = ActiveSheet
  12.     Dim i, j As Integer
  13.     Dim key As Variant
  14.     Dim item, ele As Variant
  15.    
  16.     ' 设置要检查的范围
  17.     Set rng = ws.Range("A3:DR200") ' 假设我们在Sheet1的A1:A10范围内查找
  18.     count = 0
  19.    
  20.     ' 创建正则表达式对象
  21.     Set regex = CreateObject("VBScript.RegExp")
  22.    
  23.     ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
  24.     ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
  25.     With regex
  26.         .Global = True
  27.         .IgnoreCase = True
  28.         .pattern = "[A-Za-z][0-9]+"
  29.     End With
  30.    
  31.         ' 创建正则表达式对象
  32.     Set regex1 = CreateObject("VBScript.RegExp")
  33.    
  34.     ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
  35.     ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
  36.     With regex1
  37.         .Global = True
  38.         .IgnoreCase = True
  39.         .pattern = "\d+"
  40.     End With
  41.    
  42.     ' 遍历单元格
  43.     For Each cell In rng
  44.         If regex.Test(cell.value) Then ' 如果单元格匹配正则表达式
  45.             count = count + 1 ' 增加计数
  46.             Debug.Print "行:" & cell.Row & ",列:" & cell.Column & ",值:" & cell.value
  47.             'Set matches = regex1.Execute(cell.Value)
  48.             'For Each match In matches
  49.                 'outputString = outputString & match.Value
  50.                 'Debug.Print outputString
  51.             'Next match
  52.             'Debug.Print cell.Row & cell.Column
  53.             'Debug.Print Cells(cell.Row, cell.Column).Value
  54.             Set matches = regex1.Execute(Cells(cell.Row, cell.Column).value)
  55.             For Each match In matches
  56.                 myCollection.Add match
  57.             Next match
  58.         End If
  59.     Next cell
  60.         
  61.     For Each item In myCollection
  62.         'Debug.Print item
  63.     Next
  64.     countkey = myCollection.count
  65.     For i = myCollection.count To 1 Step -1
  66.         For j = 1 To i - 1
  67.             If myCollection(i) = myCollection(j) Then
  68.                 myCollection.Remove (i)
  69.                 countkey = countkey - 1
  70.                 Exit For
  71.             End If
  72.         Next j
  73.     Next i
  74.     For Each ele In myCollection
  75.         Debug.Print ele
  76.     Next ele
  77.     ws.Cells(2, 69).value = countkey
  78.    
  79.     ' 显示计数结果
  80.     MsgBox "有 " & countkey & " 个单元格符合指定格式。"
  81. End Sub
复制代码

总结

分享:
读过的书是不是有许多不记得了,但是它不绝都是匿伏的,它在我们的出言有尺上,嬉闹有度上,服务有余上,语言有德上
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表