英文:
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()
' RegEx variables
Dim RegNum As Object
Dim RegMatches As Variant
Dim myText As String
Dim GetPercentageNumber As Double
myText = "20% (Less applicable fees and taxes)" ' <-- for testing purposes
' Late binding of setting RegEx object
Set RegNum = CreateObject("VBScript.RegExp")
' to remove all non-digits
With RegNum
.Pattern = "(\d+\.\d+)" ' Match any decimal value
.Global = True
End With
' check if a cell with a decimal value
Set RegMatches = RegNum.Execute(myText)
If RegMatches.Count >= 1 Then ' make sure there is at least 1 match
GetPercentageNumber = RegMatches(RegMatches.Count - 1)
Else ' failed to retrieve a decimal value --> check if a String with a %
' to remove all non digits
With RegNum
.Pattern = "\d{1,9}" ' Match any set of 1 to 9 digits
.Global = True
End With
Set RegMatches = RegNum.Execute(myText)
If RegMatches.Count >= 1 Then ' 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("%",A1,1))))
来完成这个任务。
关键是使用 ISNUMBER()
来判断内容是否为数字。
在VBA中,你可以使用 IsNumeric()
函数来完成相同的操作。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论