周一至周五 : 08:30 - 17:30 客服专员电话/微信:17301649370 QQ:2982200781
微信咨询

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
判断字符串中中文英文以及数字的数量
显示全部楼层 倒序浏览 发表日期 2026-03-25 11:58:47 30次阅读 0次回复


下面的VBA判断了单元格字符串中是包含中文、英文以及数字的数量。供参考使用

  1. Imports SpreadsheetGear
  2. Imports C1.Silverlight
  3. Imports C1.Silverlight.C1MessageBoxButton
  4. Imports C1.Silverlight.C1MessageBoxIcon
  5. Imports Newtonsoft.Json
  6. Imports Newtonsoft.Json.Linq
  7. Imports Microsoft.VisualBasic
  8. Imports System.IO
  9. Imports System.Text
  10. Imports System.Net
  11. Imports System.Windows.forms
  12. Namespace ExcelViewVBDotnet
  13.         Public Class StandardInterface
  14.                 '预留位置1
  15.                 '预留位置2
  16.                 Dim EV As SpreadsheetGear.Windows.Controls.WorkbookView, AW As SpreadsheetGear.IWorkbook, Range As SpreadsheetGear.IRange
  17.                 Public Sub Workbook_Open(OldRoot As Object, NewRoot As Object, Excel As SpreadsheetGear.Windows.Controls.WorkbookView)
  18.                         EV = Excel
  19.                         Excel.GetLock()
  20.                         AW = Excel.ActiveWorkbook
  21.                         Range = AW.ActiveWorksheet.Range
  22.                         Excel.ReleaseLock()
  23.                 End Sub'打开时执行事件

  24.                 Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeSelectionChangedEventArgs)
  25.                         Dim chineseCount, englishCount, numberCount As Integer
  26.                         Dim mtext As String = e.RangeSelection.Text
  27.                         GetCharacterStats(mtext, chineseCount, englishCount, numberCount)
  28.                         messagebox.show("C:" + chineseCount.ToString + " " + "E:" + englishCount.ToString + "N:" + numberCount.ToString)
  29.                 End Sub'单元格选择后执行的事件

  30.                 Public Sub RangeChanged(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeChangedEventArgs)

  31.                 End Sub'单元格编辑完成后执行事件

  32.                 Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Controls.ShapeActionEventArgs)

  33.                 End Sub'按钮/标签点击事件

  34.                 Public Sub FollowHyperlink(sender As Object)

  35.                 End Sub '暂不支持

  36.                 Public Sub GetCharacterStats(inputStr As String, ByRef chineseCount As Integer, ByRef englishCount As Integer, ByRef numberCount As Integer)
  37.                         chineseCount = 0
  38.                         englishCount = 0
  39.                         numberCount = 0

  40.                         For Each ch As Char In inputStr
  41.                                 Dim code As Integer = AscW(ch)

  42.                                 ' 判断中文字符(常用中文范围)
  43.                                 If code >= &H4E00 AndAlso code <= &H9FFF Then
  44.                                         chineseCount += 1
  45.                                 ' 判断英文字母
  46.                                 ElseIf (code >= &H41 AndAlso code <= &H5A) OrElse 'a-z
  47.                                         (code >= &H61 AndAlso code <= &H7A) Then
  48.                                         englishCount += 1
  49.                                 ' 判断数字 0-9
  50.                                 ElseIf code >= &H30 AndAlso code <= &H39 Then      ' 0-9
  51.                                         numberCount += 1
  52.                                 End If
  53.                         Next
  54.                 End Sub

  55.         End Class
  56.         '  注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
  57. End Namespace
复制代码









您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

快表软件是国内较早研究表格类软件开发平台的团队之一,迄今已有十多年的行业经验.致力于为企事业单位提供实用可靠的数字化平台。
  • 微信公众号

  • 微信小商店

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2022-2024, XiRong Soft. | 快表软件
  • 沪ICP备13033196号 | 营业执照 |上海西戎软件科技有限公司|沪公网安备31011502002146号|沪ICP备13033196号 |