2013-05-16 86 views
1

下面的代码有问题。它正确地显示了输入框,当用户输入范围J1:J503中的值9或10时,但输入框输出没有按照我的意图显示在列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 

回答

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 
+0

这是完美的。非常感谢 – SpoonTea

相关问题