提取百分比数字

huangapple go评论94阅读模式
英文:

VBA extract percentage number

问题

我需要从单元格A1中提取百分比数字。
单元格中的内容各不相同,可能是"20% (减去适用费用和税款)",也可能是纯数字,比如90%100%。我将稍后使用这个数字进行一些计算。

我的想法是首先获取%的位置,这样我就可以得到数字。

然而,如果是一个纯数字,我将无法获取百分比标记的位置,因为范围A1的值为0.9或1。

如果不是纯数字,我将能够获取百分比的位置并提取百分比数字,但它是一个字符串,我无法用它来进行计算。

有人能给我一些建议吗?非常感谢!

英文:

I need to extract the percentage number from cell A1.
The content in the cell varies, it could be "20% (Less applicable fees and taxes)", or pure number like 90%, or 100%. I will later use the number to perform some calculation.

My idea was to firstly get the position of %, so I can get the number.

However, if it is a pure number, I will not be able to get the position of the percentage mark because the value of range A1 is 0.9 or 1.

If it is not a pure number, I would be able to get the position of the percentage and extract the percentage number, but it is a string, I can't use it to perform calculation.

Can anyone give me an idea? thank you very much in advance!

答案1

得分: 2

你可以使用Range.Text获取单元格的字符串值。

Dim rg1 As Range
Dim rg2 As Range

Set rg1 = Range("A1")   '20% (Less applicable fees and taxes)
Set rg2 = Range("A2")   '90%

Debug.Print rg1.Value
Debug.Print rg2.Value

Debug.Print Val(Left(rg1.Text, InStr(1, rg1.Text, "%", vbBinaryCompare) - 1))
Debug.Print Val(Left(rg2.Text, InStr(1, rg2.Text, "%", vbBinaryCompare) - 1))

输出:

20% (Less applicable fees and taxes)
90%

20
90
英文:

You can get the cell string value with Range.Text.

Dim rg1 As Range
Dim rg2 As Range

Set rg1 = Range("A1")   '20% (Less applicable fees and taxes)
Set rg2 = Range("A2")   '90%

Debug.Print rg1.Value
Debug.Print rg2.Value

Debug.Print Val(Left(rg1.Text, InStr(1, rg1.Text, "%", vbBinaryCompare) - 1))
Debug.Print Val(Left(rg2.Text, InStr(1, rg2.Text, "%", vbBinaryCompare) - 1))

Output:

20% (Less applicable fees and taxes)
 0,9 

 20 
 90

答案2

得分: 1

你可以使用正则表达式(RegEx),它可以帮助你提取字符串中的数字部分。

Sub GetPercentageNumber()

' 正则表达式变量
Dim RegNum As Object
Dim RegMatches As Variant
Dim myText As String
Dim GetPercentageNumber As Double

myText = "20% (Less applicable fees and taxes)"  ' <-- 用于测试的字符串

' 创建正则表达式对象
Set RegNum = CreateObject("VBScript.RegExp")

' 匹配所有数字
With RegNum
    .Pattern = "(\d+\.\d+)" ' 匹配任何十进制数字
    .Global = True
End With

' 检查是否有包含十进制数字的单元格
Set RegMatches = RegNum.Execute(myText)
If RegMatches.Count >= 1 Then ' 确保至少有一个匹配项
    GetPercentageNumber = RegMatches(RegMatches.Count - 1)

Else ' 未能提取十进制数字 --> 检查是否有包含百分比的字符串
    ' 匹配所有数字
    With RegNum
        .Pattern = "\d{1,9}" ' 匹配1到9位数字
        .Global = True
    End With

    Set RegMatches = RegNum.Execute(myText)
    If RegMatches.Count >= 1 Then ' 确保至少有一个匹配项
        GetPercentageNumber = RegMatches(RegMatches.Count - 1)
    End If
End If

End Sub
英文:

You can use RegEx, which will allow you to extract just the numeric portion of whatever string.

Sub GetPercentageNumber()    

&#39; RegEx variables
Dim RegNum As Object
Dim RegMatches As Variant
Dim myText As String
Dim GetPercentageNumber As Double


myText = &quot;20% (Less applicable fees and taxes)&quot;  &#39; &lt;-- for testing purposes

&#39; Late binding of setting RegEx object
Set RegNum = CreateObject(&quot;VBScript.RegExp&quot;)
   
&#39; to remove all non-digits
With RegNum
    .Pattern = &quot;(\d+\.\d+)&quot; &#39; Match any decimal value
    .Global = True
End With
              
&#39; check if a cell with a decimal value
Set RegMatches = RegNum.Execute(myText)
If RegMatches.Count &gt;= 1 Then &#39; make sure there is at least 1 match
    GetPercentageNumber = RegMatches(RegMatches.Count - 1)

Else &#39; failed to retrieve a decimal value --&gt; check if a String with a %
    &#39; to remove all non digits
    With RegNum
        .Pattern = &quot;\d{1,9}&quot; &#39; Match any set of 1 to 9 digits
        .Global = True
    End With
    
    Set RegMatches = RegNum.Execute(myText)
    If RegMatches.Count &gt;= 1 Then &#39; make sure there is at least 1 match
        GetPercentageNumber = RegMatches(RegMatches.Count - 1)
    End If
End If
  
End Sub

答案3

得分: 1

我可以使用单元格方程 =IF(ISNUMBER(A1),A1,VALUE(LEFT(A1,FIND(&quot;%&quot;,A1,1)))) 来完成这个任务。

提取百分比数字

关键是使用 ISNUMBER() 来判断内容是否为数字。

在VBA中,你可以使用 IsNumeric() 函数来完成相同的操作。

英文:

I was able to do this using the cell equation =IF(ISNUMBER(A1),A1,VALUE(LEFT(A1,FIND(&quot;%&quot;,A1,1))))

提取百分比数字

The trick is the use ISNUMBER() to find out if the contents are numeric or not.

In VBA you can use the IsNumeric() function to do the same.

huangapple
  • 本文由 发表于 2023年5月17日 17:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定