2013-05-16 49 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

相關問題