Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如果';否';是否在消息框中按下?_Vba_Excel_Vlookup - Fatal编程技术网

Vba 如果';否';是否在消息框中按下?

Vba 如果';否';是否在消息框中按下?,vba,excel,vlookup,Vba,Excel,Vlookup,程序的功能是在要查找的值所在的同一单元格中返回vlookup的结果,但它有两个问题 1。如果在消息框中按“否”,它会一次又一次地返回消息框? 2。如果在空白单元格上按enter键,我不希望出现消息框? 这是密码 Sub Worksheet_Change(ByVal Target As Range) Dim Ret_type As Integer Dim strMsg As String Dim strTitle As String If Target.Count > 1

程序的功能是在要查找的值所在的同一单元格中返回vlookup的结果,但它有两个问题

1。如果在消息框中按“否”,它会一次又一次地返回消息框?

2。如果在空白单元格上按enter键,我不希望出现消息框?

这是密码

 Sub Worksheet_Change(ByVal Target As Range)

  Dim Ret_type As Integer
  Dim strMsg As String
  Dim strTitle As String
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Range("A1:A114")) Is Nothing Then
     Application.EnableEvents = False
     Table2 = Sheet2.Range("C2:D3")
     strTitle = "Alert"
     strMsg = "Combination not found press Yes for manual entry"

     On Error Resume Next
        Target.Value = Application.WorksheetFunction.VLookup(Target.Value, Table2, 2, False)
         Application.EnableEvents = True
         If Err.Number <> 0 Then
           Ret_type = MsgBox(strMsg, vbYesNo, strTitle)
              Select Case Ret_type
               Case 7
                ActiveCell.Offset(-1, 0).Clear
              End Select
         End If
         On Error GoTo 0 ''no error, coming back to default conditions 
  End If
End Sub
子工作表\u更改(ByVal目标作为范围)
Dim Ret_类型为整数
作为字符串的Dim strMsg
像线一样暗的线
如果Target.Count>1,则退出Sub
如果不相交(目标,范围(“A1:A114”))则为零
Application.EnableEvents=False
表2=表2.范围(“C2:D3”)
strTitle=“警报”
strMsg=“未找到组合按Yes手动输入”
出错时继续下一步
Target.Value=Application.WorksheetFunction.VLookup(Target.Value,表2,2,False)
Application.EnableEvents=True
如果错误号为0,则
Ret_type=MsgBox(strMsg、vbYesNo、strTitle)
选择案例检索类型
案例7
ActiveCell.Offset(-1,0)。清除
结束选择
如果结束
出现错误时转到0''无错误,返回默认条件
如果结束
端接头

1:确保仅在代码每次更改后重新启用事件。否则,它将继续在递归中重新启动自身

2:在代码开头检查目标是否为空

+1:不要像这样在宏中使用activecell.offset;您的用户可以单击任何单元格,在这种情况下,偏移量没有任何意义。改用目标变量

你的代码相当混乱,我不确定它在这一点上是否完全有意义。无论如何,这里有一个工作版本。有关兴趣点,请参阅我的在线评论

我还简化了一些,删除了一些无用的结构

 Sub Worksheet_Change(ByVal Target As Range)

  Dim strMsg As String
  Dim strTitle As String

  If Target.Cells.Count > 1 Then Exit Sub
  If Target.Value = "" Then Exit Sub 'don't bother with a blank cell

  If Not Intersect(Target, Range("A1:A114")) Is Nothing Then
    Application.EnableEvents = False
    Table2 = Sheet2.Range("C2:D3")
    strTitle = "Alert"
    strMsg = "Combination not found press Yes for manual entry"

    On Error Resume Next
      Target.Value = Application.WorksheetFunction.VLookup(Target.Value, Table2, 2, False)
      If Err.Number <> 0 Then
        If MsgBox(strMsg, vbYesNo, strTitle) = vbYes Then
          Target.Clear 'Better than the offset version; this works also if your user uses tab or mouse click, not only enter
        End If
      End If
    On Error GoTo 0
    Application.EnableEvents = True 'only re-enable events AFTER having changed everything you want to change
  End If
End Sub
子工作表\u更改(ByVal目标作为范围)
作为字符串的Dim strMsg
像线一样暗的线
如果Target.Cells.Count>1,则退出Sub
如果Target.Value=“”,则退出Sub“不要使用空白单元格”
如果不相交(目标,范围(“A1:A114”))则为零
Application.EnableEvents=False
表2=表2.范围(“C2:D3”)
strTitle=“警报”
strMsg=“未找到组合按Yes手动输入”
出错时继续下一步
Target.Value=Application.WorksheetFunction.VLookup(Target.Value,表2,2,False)
如果错误号为0,则
如果MsgBox(strMsg,vbYesNo,strTitle)=vbYes,则
目标。清除“优于偏移版本”;如果用户使用制表符或鼠标单击,而不仅仅是输入,则此功能也有效
如果结束
如果结束
错误转到0
Application.EnableEvents=True“仅在更改了要更改的所有内容后重新启用事件”
如果结束
端接头