InputBox文本未显示在单元格中

时间:2013-05-16 07:05:13

标签: excel vba

以下代码有问题。当用户在范围J1:J503中输入值9或10时,它会正确显示InputBox,但InputBox输出不会像我预期的那样显示在L列中。为什么呢?

Private Sub Worksheet_Change(ByVal Target As Range)

Dim vrange As Range, cell As Range
  Dim TheAnswer$
  Set vrange = Range("J1:J503")
  If Intersect(vrange, Target) Is Nothing Then Exit Sub
  For Each cell In Intersect(vrange, Target)
     If cell.Value = 9 Or cell.Value = 10 Then
        Target.Offset(0, 2).Select
        TheAnswer = InputBox("Please put comments", "Comments required for option 9 and 10")
     End If
  Next cell
End Sub

1 个答案:

答案 0 :(得分:0)

那是因为你没有告诉它在L栏中显示任何内容。

为此,您可以在End If之前添加此行代码:

Selection.Value = TheAnswer

稍微重构一下你的代码(inline temp并摆脱.Selection,在我看来,没有人应该使用{:}}:

Dim vrange As Range, cell As Range
Set vrange = Range("J1:J503")
If Intersect(vrange, Target) Is Nothing Then Exit Sub
For Each cell In Intersect(vrange, Target)
    If cell.Value = 9 Or cell.Value = 10 Then
        With Target.Offset(0, 2)
            .Value = InputBox("Please put comments", "Comments required for option 9 and 10")
            .Select ' do you really need this? If not, get rid of it
        End With
    End If
Next cell