2017-10-04 90 views
0

在下面的excel 2010 VBA我試圖創建一個用戶菜單,它在變量strInput中存儲了一個選擇(只有2個選項)。該選擇將在稍後使用,但我不確定是否在下面這樣做。該部分的1或2是存儲的還是唯一的5位數字代碼?有沒有更好的辦法?非常感謝你:)。excel 2010 vba用戶菜單將變量存儲爲變量

VBA

Dim Msg, Title As String 
Dim MyInput As Integer 

' Define message." 
Msg = "Which array was used ? " _ 
& vbNewLine & "Enter 1 for Design: 00000" & vbNewLine _ 
& "Enter 2 for Design: 11111" 

Title = "Selection of Application" ' Define title. 

While MyInput.Value <> 1 And MyInput.Value <> 2 

MyInput = InputBox(Msg, Title) 
Select Case MyInput 
    Case 1 
     MsgBox "User chose Design 1" 
    Case 2 
     MsgBox "User chose Design 2" 
    Case Else 
     MsgBox 「Invalid Entry. Try again.」 
End Select 

Wend 

'STORE SELECTION ' 
Dim strInput As Integer 
strInput = MyInput.Value 

' CONVERT USER CHOICE ' 
If MyInput = 1 Then 
strInput = "00000" 
Else 
strInput = "11111" 
End If 

期望:

用戶選擇1,因此00000將被存儲在strInput但用戶選擇2,所以​​將被存儲在strInput

回答

1

的用戶輸入的值被存儲,但是這可能不是1或2,因爲對i沒有限制禁止用戶輸入不正確的值。嘗試下面的代碼,不斷詢問問題,直到用戶輸入了有效的數字(1或2)。

Dim Msg, Title As String 
Dim MyInput As Integer 

' Define message." 
Msg = "Which array was used ? " _ 
& vbNewLine & "Enter 1 for Design: 00000" & vbNewLine _ 
& "Enter 2 for Design: 11111" 

Title = "Selection of Application" ' Define title. 

While MyInput.Value <> 1 And MyInput.Value <> 2 

    MyInput = InputBox(Msg, Title) 
    Select Case MyInput 
     Case 1 
      MsgBox "User chose Design 1" 
     Case 2 
      MsgBox "User chose Design 2" 
     Case Else 
      MsgBox 「Invalid Entry. Try again.」 
    End Select 

Wend 

'STORE SELECTION ' 
Dim strInput As Integer 
strInput = MyInput.Value 
+0

我得到'While MyInput'無效限定符。謝謝 :)。 – Chris

+0

我刪除了'.value',錯誤消失了,但看起來像1或2被存儲,而不是'00000'或'11111',是因爲'.value'?謝謝 )。 – Chris

+0

我在我的帖子中編輯了'vba'來轉換用戶選擇(或者我認爲)。這是正確的/正確的方式?謝謝 :)。 – Chris