我試圖更改代碼但它沒有用。這是我的代碼:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text - CLtotaloutput.Caption)
If Val(bill) > 0 Then
Change.CLchange.Caption = Val(bill)
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
End If
If Val(bill) < 0 Then
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
End If
End If
End Sub
突出顯示的代碼
bill = Val(CTcash.Text - CLtotaloutput.Caption)
uj5u.com熱心網友回復:
CTcash.Text 和 CLTotaloutput.Caption 都是文本字串,因此不能直接減去它們。嘗試:
bill = Val(CTcash.text) - Val(CLtotaloutput.Caption)
不過,您確實應該確保兩個字串都首先是數字!
uj5u.com熱心網友回復:
起初我想念常量,我把它放在另一個子中:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - totalsum
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
End Select
End If
End Sub
Private Sub CTbarcode_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Is = vbKeyF6
totaldiscount = 200
totalsum = Val(CLsuboutput.Caption) - totaldiscount
CLtotaloutput.Caption = Format(totalsum, "Rp\. ###,###,###. ,-")
CLdiscoutput.Caption = Format(totaldiscount, "Rp\. ###,###,###. ,-")
CTcash.Enabled = True
CTcash.SetFocus
End Select
End Sub
這是我編輯后的:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
totaldiscount = 200
totalsum = Val(CLsuboutput.Caption) - totaldiscount
bill = Val(CTcash.Text) - totalsum
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
End Select
End If
End Sub
uj5u.com熱心網友回復:
@John Eason 是正確的。您也不必使用Valon,bill因為它已經是一個整數。如果 bill 為 0,您將不執行任何操作,但根據您的要求,這可能沒問題。
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
If bill > 0 Then ' Note that when bill is 0 nothing happens but that might be fine
'Change.CLchange.Caption = bill 'Not sure why this is needed but it may be
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
End If
If bill < 0 Then
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
End If
End If
End Sub
我認為更好的方法是使用Select Case陳述句。
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
' Do nothing - this documents that this is done on purpose
End Select
End If
End Sub
您可以使用IsNumeric函式來檢查輸入是否為數字并進行相應的決議。使用該Right函式從左側洗掉任何非數字。例如,Right("$123", Len("$123") - 1)將洗掉前導$.
從字串中去除所有非數字值:
Dim ResultString As String
myString = "aaa34BB12,000xcv9.9zz"
Dim i As Integer
For i = 1 To Len(myString)
myChar = Mid(myString, i, 1)
If IsNumeric(myChar) = True Then
ResultString = ResultString myChar
End If
Next
MsgBox ResultString
請注意,這可能會導致小數點出現問題,因為 1.23 會在 123 之前。這不是您想要的。嘗試使用該CCur功能。我自己從未使用過它,但它可能正是您想要的。
來源:https : //www.vbforums.com/showthread.php?437526-RESOLVED-Removing-non-numeric-chars-from-a-string&p=2686411&viewfull=1#post2686411
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/326945.html
