当前位置: 首页 > article >正文

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

系列文章目录

文章目录

  • 系列文章目录
  • 前言
  • 一、按需求计数单元格数量
    • 1.需求
  • 二、使用步骤
    • 1.vba源码
    • 2.整理后
  • 总结


前言

一、按需求计数单元格数量

1.需求

一个表中有多个类型的单元格内容,比如:文字、数字、特殊字符、字母+数字……
我们要计数字母+数字的单元格数量
同时提取字母+数字单元格的数字部分,判断数字部分是否相同,然后计数不同的单元格数量

二、使用步骤

1.vba源码

代码如下(示例):

Sub CountSpecificFormatStrings()
    Dim rng As Range
    Dim cell As Range
    Dim count, countkey As Integer
    Dim regex, regex1 As Object
    Dim cellAddress As String
    Dim numbers As String
    Dim matches As Object
    Dim match As Variant
    Dim cellNumber As String
    Dim cellValue As String
    Dim myCollection As Collection
    Set myCollection = New Collection
    Dim value As Variant
    Dim lastValue As Variant
    Dim uniqueCount As Integer
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim cellValueCollection As Collection
    Dim i, j, k As Integer
    Dim valueToAdd As String
    Dim key As Variant
    Dim dict As Object
    Dim myArray As Variant
    Dim item, ele As Variant
    Dim elements As Variant
    Dim uniquekeys As New Collection

    uniqueCount = 0
    lastValue = ""
    Set cellCollection = New Collection
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' 设置要检查的范围
    Set rng = ws.Range("A3:DR200") ' 假设我们在Sheet1的A1:A10范围内查找
    count = 0
    
    ' 创建正则表达式对象
    Set regex = CreateObject("VBScript.RegExp")
    
    ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
    ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
    With regex
        .Global = True
        .IgnoreCase = True
        .pattern = "[A-Za-z][0-9]+"
    End With
    
        ' 创建正则表达式对象
    Set regex1 = CreateObject("VBScript.RegExp")
    
    ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
    ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
    With regex1
        .Global = True
        .IgnoreCase = True
        .pattern = "\d+"
    End With

    ' 遍历单元格
    For Each cell In rng
        If regex.Test(cell.value) Then ' 如果单元格匹配正则表达式
            count = count + 1 ' 增加计数
    
            Debug.Print "行:" & cell.Row & ",列:" & cell.Column & ",值:" & cell.value
            
            'Set matches = regex1.Execute(cell.Value)
            'For Each match In matches
                'outputString = outputString & match.Value
                'Debug.Print outputString
            'Next match
            'Debug.Print cell.Row & cell.Column
            'Debug.Print Cells(cell.Row, cell.Column).Value
            Set matches = regex1.Execute(Cells(cell.Row, cell.Column).value)
            For Each match In matches
                myCollection.Add match
            Next match
            
        End If
    Next cell
    
    
    For Each item In myCollection
        'Debug.Print item
    Next
    
    countkey = myCollection.count
    
    For i = myCollection.count To 1 Step -1
        For j = 1 To i - 1
            If myCollection(i) = myCollection(j) Then
                myCollection.Remove (i)
                countkey = countkey - 1
                Exit For
            End If
        Next j
    Next i
    
    For Each ele In myCollection
        Debug.Print ele
    Next ele

    ws.Cells(2, 69).value = countkey

    ' 显示计数结果
    MsgBox "有 " & countkey & " 个单元格符合指定格式。"
End Sub

2.整理后

代码如下(示例):

Sub CountSpecificFormatStrings()
    Dim rng As Range
    Dim cell As Range
    Dim count, countkey As Integer
    Dim regex As Object
    Dim matches As Object
    Dim match As Variant
    Dim myCollection As Collection
    Set myCollection = New Collection
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim i, j As Integer
    Dim key As Variant
    Dim item, ele As Variant
    
    ' 设置要检查的范围
    Set rng = ws.Range("A3:DR200") ' 假设我们在Sheet1的A1:A10范围内查找
    count = 0
    
    ' 创建正则表达式对象
    Set regex = CreateObject("VBScript.RegExp")
    
    ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
    ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
    With regex
        .Global = True
        .IgnoreCase = True
        .pattern = "[A-Za-z][0-9]+"
    End With
    
        ' 创建正则表达式对象
    Set regex1 = CreateObject("VBScript.RegExp")
    
    ' 设置正则表达式模式:一个字母后面跟随任意数量的数字
    ' 这里的模式是 [A-Za-z][0-9]+,其中 [A-Za-z] 匹配任意一个字母,[0-9]+ 匹配一个或多个数字
    With regex1
        .Global = True
        .IgnoreCase = True
        .pattern = "\d+"
    End With
    
    ' 遍历单元格
    For Each cell In rng
        If regex.Test(cell.value) Then ' 如果单元格匹配正则表达式
            count = count + 1 ' 增加计数
            Debug.Print "行:" & cell.Row & ",列:" & cell.Column & ",值:" & cell.value
            'Set matches = regex1.Execute(cell.Value)
            'For Each match In matches
                'outputString = outputString & match.Value
                'Debug.Print outputString
            'Next match
            'Debug.Print cell.Row & cell.Column
            'Debug.Print Cells(cell.Row, cell.Column).Value
            Set matches = regex1.Execute(Cells(cell.Row, cell.Column).value)
            For Each match In matches
                myCollection.Add match
            Next match
        End If
    Next cell
        
    For Each item In myCollection
        'Debug.Print item
    Next
    countkey = myCollection.count
    For i = myCollection.count To 1 Step -1
        For j = 1 To i - 1
            If myCollection(i) = myCollection(j) Then
                myCollection.Remove (i)
                countkey = countkey - 1
                Exit For
            End If
        Next j
    Next i
    For Each ele In myCollection
        Debug.Print ele
    Next ele
    ws.Cells(2, 69).value = countkey
    
    ' 显示计数结果
    MsgBox "有 " & countkey & " 个单元格符合指定格式。"
End Sub

总结

分享:
读过的书是不是有很多不记得了,但是它一直都是潜在的,它在我们的出言有尺上,嬉闹有度上,做事有余上,说话有德上


http://www.kler.cn/a/429700.html

相关文章:

  • 微信小程序获取当前页面路径,登录成功后重定向回原页面
  • C#,图论与图算法,输出无向图“欧拉路径”的弗勒里(Fleury Algorithm)算法和源程序
  • 七大排序算法(Java,便于理解)
  • SpringBoot-Day1
  • Redis快速入门店铺营业状态设置
  • hutool糊涂工具通过注解设置excel宽度
  • vue之$emit 获取返回值
  • 数字孪生与大型模型强强联合,共塑工业制造崭新前景
  • .NET用C#导入Excel数据到数据库
  • 面试技术点之安卓篇
  • 游戏AI实现-有限状态机
  • 通过Zynq FPGA对雷龙SD NAND进行测试
  • 黑马商城docker部署部分MySQL拉取超时解决方法
  • 前端学习纪要
  • java八股-流量封控系统
  • Leetcode 每日一题 1.两数之和
  • Linux图形化工具推荐
  • 【sgUploadImage】自定义组件:基于elementUI的el-upload封装的上传图片、相片组件,适用于上传缩略图、文章封面
  • 【Linux】08 -- 重定向命令及管道命令
  • mac下flutter开发环境的配置
  • CGAL自相交修复测试
  • 使用Python3 连接操作 OceanBase数据库
  • 碰撞算法8 --直线与圆的碰撞
  • UART+DDR3+HDMI联合图像存储与显示系统
  • git 过滤检出包含windows平台不兼容文件
  • FSC认证是什么?FSC认证费用