2016-12-27 82 views
0

工作我是自學的VBA,並試圖寫一個小程序執行以下操作IF語句不VBA

1.asks用戶在文本框
2.當進入10之間的號碼20按鈕被點擊,代碼將檢查輸入到文本框中的數字。如果它在10到20之間,則會顯示一條消息。如果輸入的數字不在10和20之間,那麼用戶將被邀請嘗試增益,並且無論在文本框中輸入的內容都將被刪除。

Private Sub Command0_Click() 
Me.Text3.SetFocus 
inumber = Val(Text3.Text) 
If inumber >= 10 & inumber <= 20 Then 
MsgBox ("The number you entered is: ") & inumber 
Else 
Text3.Text = "" 
MsgBox ("Please try again") 
End If 

End Sub 

但是,我不認爲我的代碼的其他部分正在工作。如果我輸入5,它將顯示5而不是消息框。任何人都可以讓我知道,如果我錯過了什麼。

在此先感謝。節日快樂。

+0

'某處,那麼'If'應該是'如果Form_customer_test> = 10並且數字<= 20然後'。但我認爲你的意思是,如果inumber> = 10並且數字<= 20 Then' –

回答

1

下面嘗試。 &用於VB中的字符串連接。 And應在VB中使用

Private Sub Command0_Click() 
    Me.Text3.SetFocus 
    inumber = Val(Text3.Text) 
    If inumber >= 10 And inumber <= 20 Then 
     MsgBox ("The number you entered is: ") & inumber 
    Else 
     Text3.Text = "" 
     MsgBox ("Please try again") 
    End If 
End Sub 
+0

非常感謝!就是這樣!謝謝,它現在有效。 :) –

1
If Form_customer_test >= 10 & inumber <= 20 Then 

應該是:

If inumber >= 10 & inumber <= 20 Then 
+0

謝謝指出。我很抱歉,那是一個錯字,我實際上是以數字而不是form_customer_test。我不知道那是怎麼回事。謝謝 –

1

確認:如果要定義`Form_customer_test使用 「和」 或 「或」 邏輯運算符