返回提示值

时间:2017-07-18 08:26:31

标签: excel vba excel-vba

您好我正在处理一个代码,该代码会将每个案例的提示值返回到一个单元格(" A1")但我似乎无法将其弄好,例如如果我选择"案例1",我希望单元格上的值" A1"成为" 1 - FCI18-0",有没有办法做到这一点?以下是我的代码:

Sub TestFunction()
ans = InputBox("1 = FCI18-0" & vbCrLf & _
               "2 = FCI18-1" & vbCrLf & _
               "3 = FCI18-2" & vbCrLf & _
               "4 = FCI18-3", "Model")

Sheets("Sheet1").Range("a1").Value = InputBoxPrompt
End Sub

请帮忙

2 个答案:

答案 0 :(得分:1)

使用Select Case

Sub TestFunction()
    ans = InputBox("1 = FCI18-0" & vbCrLf & _
                   "2 = FCI18-1" & vbCrLf & _
                   "3 = FCI18-2" & vbCrLf & _
                   "4 = FCI18-3", "Model")
    Select Case ans
        Case 1: Sheets("Sheet1").Range("a1").Value = "1 = FCI18-0"
        Case 2: Sheets("Sheet1").Range("a1").Value = "2 = FCI18-1"
        Case 3: Sheets("Sheet1").Range("a1").Value = "3 = FCI18-2"
        Case 4: Sheets("Sheet1").Range("a1").Value = "4 = FCI18-3"
    End Select
End Sub

答案 1 :(得分:1)

怎么样:

Sub TestFunction()
  'Declare variables
  Dim test(3) As String
  Dim i as long
  'Set array with cases. Advantage of this set up is flexibility. In a later stage you can 
  'make a function that reads cases from a table in a (hidden) sheet into this array. 
  'This way you don't need to add new cases to your code, 
  'but you can add them in your excel sheet instead. 
  test(0) = "FCI18-0"
  test(1) = "FCI18-1"
  test(2) = "FCI18-2"
  test(3) = "FCI18-3"

  'read the input given by the user. You might want to add some checks here
  i = InputBox ("Which case?")

  Sheets("Sheet1").Range("a1").Value = test(i-1)
End Sub