Excel VBA,当单元格中的文本更改为 "News" 以回答下一列中的 Msgbox 时如何循环 Msgbox
Excel VBA, How to Loop a Msgbox when text in cell changes to "News" to answer of Msgbox in next column
我正在尝试创建一个 MsgBox,当列中的单元格从空白变为 "News" 时自动弹出提示 "Yes or No",并将答案放入下一个柱子。
随着时间的推移,我将继续添加到行中,因此当单元格从空白变为 "news" 时它必须自动弹出并将答案输入到右侧新添加的单元格中。
我很确定我需要 For each 循环,但老实说我有点迷茫,在 If Intersect 行调试期间遇到不匹配错误。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRange As Range
Set myRange = Range("G2:G1000")
If Intersect(myRange, Target) Then
If Range("G2").Value = "News" Then Answer = MsgBox("Good?", vbYesNo)
Answer = ActiveCell.Offset(0, 1) = 1 'not sure if this is right, or is it Range.Offset?
Dim cel As Range
For Each cel In Range("G2:G1000")
If cel.Value = "News" Then Answer = MsgBox("Good?", vbYesNo)
Answer = ActiveCell.Offset(0, 1) = 1 'not sure if this is right, or is it Range.Offset?
Exit For
Next
End If
End Sub
给你:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 7 Then
If Target.Count = 1 Then
If LCase$(Target) = "news" Then
Application.EnableEvents = False
Target(, 2) = Array("Yes", "No")(MsgBox("Good?", vbYesNo) - 6)
End If
End If
End If
Application.EnableEvents = True
End Sub
我正在尝试创建一个 MsgBox,当列中的单元格从空白变为 "News" 时自动弹出提示 "Yes or No",并将答案放入下一个柱子。
随着时间的推移,我将继续添加到行中,因此当单元格从空白变为 "news" 时它必须自动弹出并将答案输入到右侧新添加的单元格中。
我很确定我需要 For each 循环,但老实说我有点迷茫,在 If Intersect 行调试期间遇到不匹配错误。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRange As Range
Set myRange = Range("G2:G1000")
If Intersect(myRange, Target) Then
If Range("G2").Value = "News" Then Answer = MsgBox("Good?", vbYesNo)
Answer = ActiveCell.Offset(0, 1) = 1 'not sure if this is right, or is it Range.Offset?
Dim cel As Range
For Each cel In Range("G2:G1000")
If cel.Value = "News" Then Answer = MsgBox("Good?", vbYesNo)
Answer = ActiveCell.Offset(0, 1) = 1 'not sure if this is right, or is it Range.Offset?
Exit For
Next
End If
End Sub
给你:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 7 Then
If Target.Count = 1 Then
If LCase$(Target) = "news" Then
Application.EnableEvents = False
Target(, 2) = Array("Yes", "No")(MsgBox("Good?", vbYesNo) - 6)
End If
End If
End If
Application.EnableEvents = True
End Sub