2014-04-08 94 views
3

我使用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 

回答

3

可以使用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")) 
+0

聰明的做法確實如此。 – L42

+0

非常感謝。 –

1

非正則表達式的解決方案:)

Sub box() 

Dim mypercent 

mypercent = InputBox("How much of an increase") 
If mypercent Like "*[0-9]" Then 
    Range("A1").Value = Range("A1").Value * (1 + (myPercent * 0.01)) 
Else 
    MsgBox "Not valid input. Enter whole numbers only" 
End If 

End Sub 
0

也試試這個代碼:

Dim myPercent 
Do 
    myPercent = InputBox("How much of an increase?", "Input without the percent symbol") 
    If Not IsNumeric(myPercent) Then MsgBox "Wrong input! Try again.." 
Loop Until IsNumeric(myPercent) 
Range("A1").Value = Range("A1").Value * (1 + (myPercent * 0.01))