Vba 将百分比转换为数字,如20%到20

Vba 将百分比转换为数字,如20%到20,vba,excel,wildcard,Vba,Excel,Wildcard,我有一个包含百分比值的列。列单元格的格式为“数字”。 这是一个演示专栏。这些是用户输入的百分比值 我尝试的代码: 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]*%") The

我有一个包含百分比值的列。列单元格的格式为“数字”。

这是一个演示专栏。这些是用户输入的百分比值

我尝试的代码:

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
            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
Sub-Percent()
变暗细胞
作为变量的值
对于选择中的每个单元格
带电池
cellValue=.Text
MsgBox单元值
如果(单元格值如“[0-9]*%”),则

.NumberFormat=“General”是否可以尝试将属性添加为:.NumberFormat=“General”。Value=.Value您可以将格式重新设置为Number并使用PasteSpecial乘法(将100放入空单元格,复制,PasteSpecial)。@NandanChaturvedi删除了cellValue=Left(cellValue,Len(cellValue)-1).Value=cellValue并添加了两行代码,效果非常好。刚刚更改了.value=,value*100,因为您的语句给了我十进制值。非常感谢。@SJR我会试试你的建议。感谢您的时间和努力。@Nandanchatarvedi将您的评论作为单独的答案发布,以便我可以将您的评论标记为其他有疑问的用户选择的答案。谢谢。我试试看。BDW,.numberFormat=“@”将做什么?
.numberFormat=“@”
将单元格设置为“文本”format@AmanDevrath只是把它调整为整数,在最初的问题中忽略了这个。谢谢你的回答。谢谢。问题中缺少一个IF语句。除此之外,非常好。谢谢。我试过了,最终的值是“文本”而不是“数字”。如何在结尾将其转换为数字?@AmanDevrath只需删除代码行中带有Left()的单引号前缀