2017-09-11 88 views
0

新手提醒。我使用MsgBoxInputBox組合,詢問用戶是否要刪除具有指定值(輸入框)的行(msgbox)。我循環它,直到我看到Msgbox中的vbNo,這似乎工作,但InputBox用戶值驗證似乎不起作用。
我讀過Inputboxes將值識別爲String,所以不確定這是否會成爲問題?帶數字的MsgBox + InputBox,使用If驗證輸入號碼

我的查詢

1)我想限制用戶只能輸入數字在輸入框中,這也是超過4個位數不再。我正在使用If語句,但沒有任何工作。我正在考慮將輸入框類型分配給整數,並且它將輸入限制爲數字,但如果用戶錯誤輸入並且不是所有人都會知道他們做錯了什麼,則不會發出警告。

2)當我把If UserValue = vbNullString Then GoTo ContinueIf UserValue <> vbNullString Then GoTo StoreEntered相反的行 - 過程不起作用,是否有區別?任何建議非常感謝。

3)UPD我只是試着點擊vbYes on Mgsbox然後Cancel on InputBox,我發誓它之前的工作,但似乎並沒有工作了。我會認爲參考vbNullString將解決不輸入任何內容並單擊確定或簡單地取消輸入框的問題。

這裏是我的代碼

Dim UserValue As Integer 
Dim UserReply As Variant 

KeepLoop: 
Do 
UserReply = MsgBox(Prompt:="Do you want any store transactions removed?", Buttons:=vbYesNo, Title:="Stores removed") 
    If UserReply = vbNo Then GoTo Continue 
    If UserReply = vbYes Then GoTo UserInputReqd 
Loop Until UserReply = vbNo 

UserInputReqd: 
UserValue = Application.InputBox(Prompt:="Enter any store number you want removed from the file. Otherwise click Cancel", _ 
       Title:="CHOSE STORES TO RUN OR DELETE") ', Type:=1) 

     If UserValue <> vbNullString Then GoTo StoreEntered 'If user left line blank and clicked 'Ok' or clicked 'Cancel' 
     If Not IsNumeric(UserValue) Then 
       MsgBox "You must enter a numeric value" 
     If Len(UserValue) > 4 Then 
       MsgBox "Max store number is 4 digits", vbCritical 
     End If 
     End If 

StoreEntered: 
r = 100000 
Do 
If Sheets(csv_sht).Cells(r, 1) = UserValue Then Sheets(csv_sht).Rows(r).EntireRow.Delete 
r = r - 1 
Loop Until r = 1 
GoTo KeepLoop 

Continue: 'Next step, continue as normal. 
==Code here== 

End Sub 

回答

0

這並不完全清楚,我確切的消息流應該是什麼,如果你不進入商店數量,但是這樣的事情應該簡化事情:

Dim UserValue    As Variant 
Dim UserReply    As Variant 
Do 
    UserReply = MsgBox(Prompt:="Do you want any store transactions removed?", Buttons:=vbYesNo, Title:="Stores removed") 
    If UserReply = vbYes Then 

     UserValue = InputBox(Prompt:="Enter any store number you want removed from the file. Otherwise click Cancel", _ 
             Title:="CHOSE STORES TO RUN OR DELETE") 
     If UserValue = vbNullString Then 
      ' do nothing 
     ElseIf Not IsNumeric(UserValue) Then 
       MsgBox "You must enter a numeric value" 
     ElseIf Len(UserValue) > 4 Then 
       MsgBox "Max store number is 4 digits", vbCritical 
     Else 
      For r = 100000 To 1 Step -1 
       If Sheets(csv_sht).Cells(r, 1) = CLng(UserValue) Then Sheets(csv_sht).Rows(r).EntireRow.Delete 
      Next r 
     End If 
    End If 
Loop Until UserReply = vbNo 
'Next step, continue as normal. 
+0

如果您沒有輸入商店編號或取消,或者單擊MsgBox上的否,它只會將您帶到標籤Continue(在底部),其中只包含與此Msgbox/InputBox不相關的其他代碼組合。非常感謝!我會盡快嘗試這個,並讓你知道 – user8449681

+0

謝謝羅裏,這是我所想的,非常感謝! – user8449681