【问题标题】:Convert percentage to numbers, like 20% to 20将百分比转换为数字,例如 20% 到 20
【发布时间】:2018-06-04 11:51:24
【问题描述】:

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

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

我试过的代码:

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

在运行此程序时,我想将“选定的单元格”转换为数字,即我想删除那个百分比符号,我不想要小数的值。只是整数(没有 % 符号)。

上面发布的代码有效,但该列应该是“文本”列。这意味着,在列的“格式单元格”选项中,它应该说文本而不是数字,然后只有我的代码有效。但是当我将“格式单元格”中的列更改为“数字”时,代码不起作用。

【问题讨论】:

  • 您可以尝试将属性添加为:.NumberFormat = "General" .Value = .Value
  • 您可以重新格式化为 Number 并使用 PasteSpecial 乘法(将 100 放在一个空单元格中,复制,pastespecial)。
  • @NandanChaturvedi 我删除了 cellValue = Left(cellValue, Len(cellValue) - 1) .Value = cellValue 并添加了你的两行,它运行良好。刚刚更改了 .value = ,value * 100,因为您的语句给了我十进制值。非常感谢。
  • @SJR 我会试试你的建议。感谢您的时间和精力。
  • @NandanChaturvedi 将您的评论作为单独的答案发布,以便我可以将您的评论标记为其他有疑问的用户的选择答案。

标签: vba excel wildcard


【解决方案1】:

你可以试试这个:

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

【讨论】:

  • 谢谢。问题中缺少一个 IF 语句。除了完美之外。
  • 谢谢。正常工作
【解决方案2】:

你可以这样做:

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

【讨论】:

    【解决方案3】:

    如果你想要数字作为文本:

    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
    

    我们:

    • 修改了%测试
    • 插入了前缀字符
    • 允许循环继续

    【讨论】:

    • 我试过了,最终值是“文本”而不​​是“数字”。最后如何将其转换为数字?
    • @AmanDevrath 只需用 Left() 去掉代码行中的单引号前缀
    【解决方案4】:

    我会先将单元格转换为数字,乘以 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
    

    【讨论】:

    • 谢谢。我会试试看。 BDW,.numberFormat = "@" 会做什么?
    • .NumberFormat = "@" 将单元格设置为“文本”格式
    • @AmanDevrath 刚刚将其调整为整数,在最初的问题中错过了
    • 谢谢你的回答。
    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2014-05-27
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多