将百分比转换为数字,例如 20% 到 20
Convert percentage to numbers, like 20% to 20
我有一列包含百分比值。列单元格的格式为 "number"。
这是一个演示专栏。这些是用户输入的百分比值。
我试过的代码:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
cellValue = Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
Exit Sub
End If
End With
Next
End Sub
关于 运行 这个,我想将 "selected cells" 转换为数字,即我想删除那个百分比符号,我不想要小数的值。只是整数(没有 % 符号)。
上面发布的代码有效,但该列应该是 "text" 列。这意味着,在列的 "format cells" 选项中,它应该说文本而不是数字,然后只有我的代码有效。但是当我将 "format cells" 中的列更改为 "number" 时,代码不起作用。
如果你想要数字作为文本:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
If Right(cellValue, 1) = "%" Then
cellValue = "'" & Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
End If
End With
Next
End Sub
我们:
- 修改了%测试
- 插入了前缀字符
- 允许循环继续
我会先将单元格转换为数字,乘以 100,然后转换为文本。
Sub Percent()
Dim Cel As Range
For Each Cel In Selection.Cells
With Cel
.NumberFormat = "0.00"
.Value2 = Round(.Value2 * 100, 0)
.NumberFormat = "@"
End With
Next Cel
End Sub
你可以试试这个:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
.NumberFormat = "0.00"
.NumberFormat = "0"
cellValue = .Value * 100
.Value = cellValue
End With
Next
End Sub
或者这个:
Sub Percent2()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
Selection.NumberFormat = "0"
cellValue = Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
Exit Sub
End If
End With
Next
End Sub
您可以按如下方式进行:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
.NumberFormat = "General" <-- convert the format into number
.Value = .Value * 100 <--
Else
Exit Sub
End If
End With
Next
End Sub
我有一列包含百分比值。列单元格的格式为 "number"。
这是一个演示专栏。这些是用户输入的百分比值。
我试过的代码:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
cellValue = Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
Exit Sub
End If
End With
Next
End Sub
关于 运行 这个,我想将 "selected cells" 转换为数字,即我想删除那个百分比符号,我不想要小数的值。只是整数(没有 % 符号)。
上面发布的代码有效,但该列应该是 "text" 列。这意味着,在列的 "format cells" 选项中,它应该说文本而不是数字,然后只有我的代码有效。但是当我将 "format cells" 中的列更改为 "number" 时,代码不起作用。
如果你想要数字作为文本:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
If Right(cellValue, 1) = "%" Then
cellValue = "'" & Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
End If
End With
Next
End Sub
我们:
- 修改了%测试
- 插入了前缀字符
- 允许循环继续
我会先将单元格转换为数字,乘以 100,然后转换为文本。
Sub Percent()
Dim Cel As Range
For Each Cel In Selection.Cells
With Cel
.NumberFormat = "0.00"
.Value2 = Round(.Value2 * 100, 0)
.NumberFormat = "@"
End With
Next Cel
End Sub
你可以试试这个:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
.NumberFormat = "0.00"
.NumberFormat = "0"
cellValue = .Value * 100
.Value = cellValue
End With
Next
End Sub
或者这个:
Sub Percent2()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
Selection.NumberFormat = "0"
cellValue = Left(cellValue, Len(cellValue) - 1)
.Value = cellValue
Else
Exit Sub
End If
End With
Next
End Sub
您可以按如下方式进行:
Sub Percent()
Dim cell As Variant
Dim cellValue As Variant
For Each cell In Selection
With cell
cellValue = .Text
MsgBox cellValue
If (cellValue Like "[0-9]*%") Then
.NumberFormat = "General" <-- convert the format into number
.Value = .Value * 100 <--
Else
Exit Sub
End If
End With
Next
End Sub