1

以下代码有问题。当用户在 range 中输入值 9 或 10 时,它会正确显示 InputBox J1:J503,但 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
4

1 回答 1

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
于 2013-05-16T07:09:57.603 回答