简体   繁体   中英

VBA vbYesNo Inquiry

I am trying to create a message box that will give the user the option to continue or stop if their search comes up with more than 1000 results. I have the message box made, but I don't know how to code the vbYes and the vbNo to either continue on with the code (vbYes) or to end the script (vbNO).

Here is my code.

Sub FindOne()

Range("B19:J5000") = ""

Application.ScreenUpdating = False

Dim k As Integer, EndPasteLoopa As Integer, searchColumn As Integer, searchAllCount As Integer
Dim myText As String
Dim totalValues As Long
Dim nextCell As Range
Dim searchAllCheck As Boolean

k = ThisWorkbook.Worksheets.Count
myText = ComboBox1.Value
Set nextCell = Range("B20")
If myText = "" Then
    MsgBox "No Address Found"
    Exit Sub
End If

Select Case ComboBox2.Value
    Case "SEARCH ALL"
        searchAllCheck = True
    Case "EQUIPMENT NUMBER"
        searchColumn = 1
    Case "EQUIPMENT DESCRIPTION"
        searchColumn = 3
    Case "DUPONT NUMBER"
        searchColumn = 6
    Case "SAP NUMBER"
        searchColumn = 7
    Case "SSI NUMBER"
        searchColumn = 8
    Case "PART DESCRIPTION"
        searchColumn = 9
    Case ""
        MsgBox "Please select a value for what you are searching by."
End Select

For I = 2 To k
    totalValues = Sheets(I).Cells(Rows.Count, "A").End(xlUp).Row
    ReDim AddressArray(totalValues) As String

    If searchAllCheck Then
        searchAllCount = 5
        searchColumn = 1
    Else
        searchAllCount = 0
    End If

    For qwerty = 0 To searchAllCount
        If searchAllCount Then
            Select Case qwerty
                Case "1"
                    searchColumn = 3
                Case "2"
                    searchColumn = 6
                Case "3"
                    searchColumn = 7
                Case "4"
                    searchColumn = 8
                Case "5"
                    searchColumn = 9
            End Select
        End If

        For j = 0 To totalValues
            AddressArray(j) = Sheets(I).Cells(j + 1, searchColumn).Value
        Next j

        If totalValues > 1000 Then
            Results = MsgBox("Your Search has Returned Over 1000 Results. Continuing Could Cause Excel to Slow Down or Crash. Do you Wish to Continue?", vbYesNo + vbExclamation, "Warning")
        End If

        If Results = vbNo Then
            End
        End If

        If Results = vbYes Then
            For j = 0 To totalValues
                If InStr(1, AddressArray(j), myText) > 0 Then
                    EndPasteLoop = 1
                    If (Sheets(I).Cells(j + 2, searchColumn).Value = "") Then EndPasteLoop = Sheets(I).Cells(j + 1, searchColumn).End(xlDown).Row - j - 1
                    For r = 1 To EndPasteLoop
                        Range(nextCell, nextCell.Offset(0, 8)).Value = Sheets(I).Range("A" & j + r, "I" & j + r).Value
                        Set nextCell = nextCell.Offset(1, 0)
                     Next r
                End If
            Next j
        Else
            End
        End If
    Next qwerty
Next I
Debug.Print tc
Application.ScreenUpdating = True
End Sub

If I understood your problem, you have to compare the "Results" variable if is vbYes or vbNo. Below a little and simple example.

If MsgBox("Continue?", vbYesNo,"Confirmation") = vbYes Then  
 'code if yes
Else     
 'End
End If

Hope that helps. ;)

vbYes is a constant, a member of an enum called VbMsgBoxResult that defines a bunch of related constants, including vbYes and vbNo .

If vbYes Then

That's like saying

If 42 Then

You have a constant expression that evaluates to a Long integer, and an If statement works with a Boolean expression that evaluates to a Boolean value (True/False).

You need to compare vbYes to something to get that Boolean expression .

If Results = vbYes Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM