【问题标题】:Excel VBA, How to Loop a Msgbox when text in cell changes to "News" to answer of Msgbox in next columnExcel VBA,当单元格中的文本更改为“新闻”以回答下一列中的 Msgbox 时,如何循环 Msgbox
【发布时间】:2015-09-19 17:07:30
【问题描述】:

我正在尝试创建一个 MsgBox,当列中的单元格从空白变为“新闻”时自动弹出“是或否”提示,并将答案放入下一列。

随着时间的推移,我将继续添加行,因此当单元格从空白变为“新闻”时它必须自动弹出,并将答案输入到右侧新添加的单元格中。

我很确定我需要 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

【问题讨论】:

  • “旧”新闻呢?你想让它每次都在所有行上运行吗?

标签: vba excel loops text msgbox


【解决方案1】:

给你:

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

【讨论】:

  • 哇非常感谢您的快速回答!它工作得很好..我挣扎了一段时间,加油!
猜你喜欢
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2018-02-13
相关资源
最近更新 更多