返回列表 上一主題 發帖

[發問] inputbox + select case 疑問

[發問] inputbox + select case 疑問

未修改前的時候,小弟只能輸入特定值(小弟設定為0),才能離開 inputbox 否則會一直重新要求輸入
後來小弟將 inputbox 修改成  Application.InputBox 就可以直接按取消離開 inputbox
這樣應該是不需要 case 0的部份 才對 可是將 case 0 也就是紅色部份拿掉後
執行時要按取消就會變成要求重新輸入 無法離開
能否有高手解惑呢????

代碼如下
Sub TA()
re: x = InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
Select Case x
Case 1
Shell "D:\vncviewer.exe 111.99.67.64 /password cimeng"
Case 2
Shell "D:\vncviewer.exe 111.99.67.84 /password cimeng"
Case 0
GoTo a
Case Else
MsgBox "請重新輸入"
GoTo re
a:
End Select
End Sub

大大好  
case 0  還是要保留
按取消時是返回 "" 空白值

Sub TA()
re: x = InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
Select Case x
Case 0, ""
Case 1
Shell "D:\vncviewer.exe 111.99.67.64 /password cimeng"
Case 2
Shell "D:\vncviewer.exe 111.99.67.84 /password cimeng"
Case Else
MsgBox "請重新輸入"
GoTo re
End Select
End Sub

TOP

本帖最後由 准提部林 於 2015-12-13 17:10 編輯

x = InputBox(~~) 
無法分辨〔未輸入〕按enter,或按〔取消〕所產生的〔空字符""〕,
可用 If StrPtr(x) = 0 Then 判斷其為按〔取消〕之動作(不管有無輸入)!但並非所有office版本適用!!!
Re: X = InputBox("請輸入選項")
If StrPtr(X) = 0 Then Exit Sub
Select Case X
  Case 1: MsgBox "選項1"
  Case 2: MsgBox "選項2"
  Case Else: GoTo Re
End Select

===========================================
x = Application.InputBox(~~) 
無法分辦輸入0按enter,或按取消之False(系統視False為0),
可用 If x & "" = "False" Then 判斷其為按〔取消〕之動作(不管有無輸入)!
這方法是強制將False轉為文字,而不是等同0的邏輯值!
Re: X = Application.InputBox("請輸入選項")
If X & "" = "False" Then Exit Sub
Select Case X
  Case 1: MsgBox "選項1"
  Case 2: MsgBox "選項2"
  Case Else: GoTo Re
End Select

TOP

  1. Sub TA()
  2.   Dim X As Variant
  3.   Dim Y As Long
  4.   
  5.   On Error Resume Next
  6.   X = Application.InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
  7.   Do While VarType(X) <> vbBoolean
  8.     Y = -1: Y = CLng(X): X = False
  9.     Select Case Y
  10.       Case 1:     'Shell "D:\vncviewer.exe 111.99.67.64 /password cimeng"
  11.       Case 2:     'Shell "D:\vncviewer.exe 111.99.67.84 /password cimeng"
  12.       Case Else:  MsgBox "請重新輸入"
  13.     End Select
  14.     X = Application.InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
  15.   Loop
  16. End Sub
複製代碼
世界那麼大,可我想去哪?

TOP

未修改前的時候,小弟只能輸入特定值(小弟設定為0),才能離開 inputbox 否則會一直重新要求輸入
後來小弟將  ...
feecshyrnd 發表於 2015-12-12 22:20
  1. Sub TA()
  2.   Dim X As Variant
  3.   Dim Y As Long
  4.   
  5.   On Error Resume Next
  6.   X = Application.InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
  7.   Do While VarType(X) <> vbBoolean
  8.     Y = -1: Y = CLng(X): X = False
  9.     Select Case Y
  10.       Case 1:     Shell "D:\vncviewer.exe 111.99.67.64 /password cimeng"
  11.       Case 2:     Shell "D:\vncviewer.exe 111.99.67.84 /password cimeng"
  12.       Case Else:  MsgBox "請重新輸入"
  13.     End Select
  14.     X = Application.InputBox("請輸入預瀏覽之電腦", , "請輸入數字 <例如 1 > 或輸入 0 取消")
  15.   Loop
  16. End Sub
複製代碼
不好意思,樓上的代碼我把你要執行的Shell操作註釋掉了。這層代碼修改正常了。
Application.InputBox按"取消"時返回False,但Inputbox按“取消”時返回的值是"",不適用本例。
世界那麼大,可我想去哪?

TOP

        靜思自在 : 【時間成就一切】時間可以造就人格,可以成就事業,也可以儲積功德。
返回列表 上一主題