如果输入到InputBox不等于范围中的值,则显示msgbox和end sub

时间:2017-11-09 20:49:13

标签: excel vba outlook

苦苦挣扎。我弹出一个输入框。然后在一系列单元格中搜索输入的值。如果找到,则设置特定值。但是,我希望它显示一个msgbox然后结束sub,如果在该范围内找不到该值。无论我尝试什么,我都无法让它发挥作用。如果“不相等”的代码不存在,只要输入的值与范围中的值匹配,它就能正常工作。当我添加“不相等”的代码时,它会启动msgbox并结束sub,但无论找到什么值,都会这样做。据我所知,它应该是一个或另一个。如果匹配,则继续。如果没有,它就结束了。但那并没有发生。这里有什么问题?感谢

Sub Test()
Dim aOutlook As Object
Dim aEmail As Object
Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
Dim strbox As String
Dim stritem As String
Dim x As Long
Dim r As Long
Dim lr, lookRng As Range
Dim findStr As String
Dim foundCell As Variant
Dim foundcell1 As Variant
Dim foundcell2 As Variant
Dim strbody As String
Dim sigstring As String
Dim signature As String
Dim findstr1 As String
Dim foundrng As Range
Dim findrange As String


Set aOutlook = CreateObject("Outlook.Application")
Set aEmail = aOutlook.CreateItem(0)


findstr1 = InputBox("Enter Email Address")
findStr = InputBox("Enter the name to find")

lr = Cells(Rows.Count, "a").End(xlUp).Row
Set lookRng = Range("B1:B" & lr)

 For x = 1 To lr
    If Range("a" & x).Value = findStr Then
    Set foundCell = Range("B" & x).Offset(0, 4)
    Set foundcell1 = Range("B" & x).Offset(0, 1)
    Set foundcell2 = Range("B" & x).Offset(0, 5)
    End If
    Next x

   For r = 1 To lr
    If Range("a" & r).Value <> findStr Then
    MsgBox "Is case-sensitive", vbExclamation, "No Match!"
    Exit Sub
    End If
    Next r

1 个答案:

答案 0 :(得分:0)

怎么样

Dim valueFound As Boolean
valueFound = False
For x = 1 To lr
    If Range("a" & x).Value = findStr Then
        Set foundCell = Range("B" & x).Offset(0, 4)
        Set foundcell1 = Range("B" & x).Offset(0, 1)
        Set foundcell2 = Range("B" & x).Offset(0, 5)
        valueFound = True
    End If
Next x

If Not valueFound Then
    MsgBox "Is case-sensitive", vbExclamation, "No Match!"
End If