gpt4 book ai didi

excel - 过滤掉百分比符号

转载 作者:行者123 更新时间:2023-12-02 16:58:23 24 4
gpt4 key购买 nike

我使用的是 Excel 2010,并且有一些代码提示用户输入百分比,然后将单元格 A1 中的值增加该百分比。如果有人遵守规则并输入一个整数百分比,那么它就可以正常工作。因此,例如,如果有人希望该值增加 30%,他们会在输入框中键入 30。

如果有人在回复中添加百分号,就会破坏整个事情。我如何修改此代码,以便它捕获百分比符号并仅根据整数进行计算。谢谢。

Sub Box1()
Dim myPercent As Integer
myPercent = InputBox("How much of an increase?", "Input without the percent symbol")
Range("A1").Value = Range("A1").Value * (1 + (myPercent * 0.01))
End Sub

最佳答案

您可以使用Replace函数去掉任何百分号:

myPercent = Replace(InputBox("How much of an increase?", "Input without the percent symbol"), "%", "")

或者您可以使用 Val 函数忽略第一个非数字字符之后的所有内容:

myPercent = Val(InputBox("How much of an increase?", "Input without the percent symbol"))

关于excel - 过滤掉百分比符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22938710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com