excel vba实现输入一个值 在另一个sheet如果这个值存在就弹出对应的提示框

excel vba实现输入一个值 在另一个sheet如果这个值存在就弹出对应的提示框

问题描述:

如sheet A有1列数字并可能继续输入 1234 ,2345,234324等

Sheet B中 1234 会对应你好,2345会对应是么,但是234324无对应值

如果在Sheet A 的某列里输入2345,那就弹出提示框“是么”

还有你的1234对应你好是怎么对应的?值在A1,对应值在B1?

https://blog.csdn.net/weixin_33774615/article/details/85787196

Dim dict
Private Sub initDict(ByVal sheetname As String) '初始化字典,A列存储键名称,B列存储对应内容
  rownum = ThisWorkbook.Sheets(sheetname).Range("A65536").End(xlUp).Row '找到表1中有多少行数据
  Set dict = CreateObject("Scripting.Dictionary")
  For i = 1 To rownum
    Key = ThisWorkbook.Sheets(sheetname).Cells(i, "A") & ""
    Value = ThisWorkbook.Sheets(sheetname).Cells(i, "B") & ""

    If dict.exists(Key) Then '出现重复键名称
       dic.Item(Key) = Value
    Else
       dict.Add Key, Value
    End If
  Next
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Column = 1 Then '只监控第一列的值,其他列自己修改这里
     Call initDict("sheet2") '初始化,根据指定sheet初始化字段
   
     Value = Trim(Target.Value)
     If dict.exists(Value) Then
        MsgBox dict.Item(Value)
     Else
        MsgBox "无对应值"
     End If
   End If
   
End Sub