|
RT: 比如汉字居中对齐,数值左对齐红色,百分比等不同类型的数据不同的处理方式。
我现在实现的是通过正则来判断,手动输入数据都能正确判断,从表格中读取的数据来判断数值类型的就匹配不到,求大佬指导一下。
正则函数如下:
Function ReSt(txt As String) As String
Set retxt = CreateObject("VBScript.RegExp")
Set renum = CreateObject("VBScript.RegExp")
Set rebai = CreateObject("VBScript.RegExp")
With retxt
.Global = False
.Pattern = "[\u4e00-\u9fa5]+"
Set m1 = .Execute(txt)
If m1.Count > 0 Then
ReSt = 1
End If
End With
With renum
.Global = False
.Pattern = "-?([1-9]\d*|0)(.[0-9]{1,2})?$"
'.Pattern = "^[0-9]+(\.?[0-9]+)?$"
Set m2 = .Execute(txt)
If m2.Count > 0 Then
ReSt = 2
End If
End With
With rebai
.Global = False
.Pattern = "-?([1-9]\d*|0)(.[0-9]{1,2})?%$"
Set m3 = .Execute(txt)
If m3.Count > 0 Then
ReSt = 3
End If
End With
End Function
调用测试过程如下:
Sub ab()
Dim k As String
k = ActiveDocument.Tables(1).Cell(2, 3).Range.Text
k = Left(k, Len(k) - 1)
MsgBox k
m1 = ReSt(k)
MsgBox m1
End Sub
测试数据如下:
匹配不到数值和百分比,手动输入数值和百分比是可以的。
或者有更加简单的方法,求大佬指导
|
|