ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 关于查找单元格区域中特定字符个数的几种VBA方法

关于查找单元格区域中特定字符个数的几种VBA方法

作者:绿色风 分类: 时间:2022-08-18 浏览:152
楼主
liuguansky
一、数组循环判断

  1. Sub 单击() '数组循环判断
  2. Dim arr, i&, j&, s&
  3. arr = Cells(1, 1).CurrentRegion.Value
  4. s = 0
  5. For i = 1 To UBound(arr, 1)
  6.   For j = 1 To UBound(arr, 2)
  7.     If arr(i, j) = "正常" Then
  8.       s = s + 1
  9.     End If
  10. Next j, i
  11. MsgBox "共有正常个数为:" & s
  12. End Sub
二、工作表函数
  1. Sub 单击1() '工作表函数
  2. MsgBox "共有正常个数为:" & Application.WorksheetFunction.CountIf(Cells(1, 1).CurrentRegion, "正常")
  3. End Sub
三、VBA filter函数
  1. Sub 单击2() 'filter函数
  2. Dim arr, i&, arrt, s&
  3. arr = Cells(1, 1).CurrentRegion.Value
  4. s = 0
  5. For i = 1 To UBound(arr, 2)
  6.   arrt = Filter(Application.Transpose(Application.Index(arr, , i)), "正常")
  7.   s = s + UBound(arrt) + 1
  8. Next i
  9. MsgBox "共有正常个数为:" & s
  10. End Sub
 
四、字典

  1. Sub 单击3() '字典
  2. Dim arr, i&, j&, dic
  3. Set dic = CreateObject("scripting.dictionary")
  4. arr = Cells(1, 1).CurrentRegion.Value
  5. For i = 1 To UBound(arr, 1)
  6.   For j = 1 To UBound(arr, 2)
  7.     If arr(i, j) <> "" Then
  8.       If dic.exists(arr(i, j)) Then
  9.         dic(arr(i, j)) = dic(arr(i, j)) + 1
  10.         Else: dic.Add arr(i, j), 1
  11.       End If
  12.     End If
  13. Next j, i
  14. If dic.exists("正常") Then
  15.   MsgBox "共有正常个数为:" & dic("正常")
  16.   Else: MsgBox "未找到正常记录"
  17. End If
  18. Set dic = Nothing
  19. End Sub

桌面.rar


欢迎大家补充。
2楼
wise
  1. Sub test()
  2. With Range("A1:G1000")
  3.     Set c = .Find("正常", LookIn:=xlValues)
  4.     If Not c Is Nothing Then
  5.         firstAddress = c.Address
  6.         Do
  7.             i = i + 1
  8.             Set c = .FindNext(c)
  9.         Loop While Not c Is Nothing And c.Address <> firstAddress
  10.     End If
  11.     MsgBox "共有正常个数为:" & i
  12. End With
  13. End Sub

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap