vba - 将百分比转换为数字,例如 20% 到 20
问题描述
这是一个演示专栏。这些是用户输入的百分比值。
我试过的代码:
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
在运行它时,我想将“选定的单元格”转换为数字,即我想删除那个百分比符号,我不想要小数的值。只是整数(没有 % 符号)。
上面发布的代码有效,但该列应该是“文本”列。这意味着,在列的“格式单元格”选项中,它应该说文本而不是数字,然后只有我的代码有效。但是当我将“格式单元格”中的列更改为“数字”时,代码不起作用。
解决方案
你可以试试这个:
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
推荐阅读
- excel - 从单元格数据中创建可变超链接
- python - 'list' 对象不能从头开始解释为 ANN 中的整数
- android - React Native Android:发布版本一直在崩溃
- mysql - 添加mysql唯一约束以应用于特定日期内的条目
- loops - Ansible readfile 并将变量连接到每一行
- sql - 如何在单词中获取自定义符号的第一部分和第二部分
- java - 理解字符串算法中的“查找最大出现的字符”
- r - 尝试使用 tidymodels 预测二进制变量等于 1 的概率
- javascript - 用百分比数字更新进度条?
- c - 在非常简单的 C windows api 程序上找不到错误 126 模块