VBA vbYesNo Inquiry

时间:2017-06-29 14:13:45

标签: excel vba msgbox

我正在尝试创建一个消息框,如果用户的搜索结果超过1000,则会为用户提供继续或停止的选项。我已经制作了消息框,但我不知道如何编码vbYes和vbNo以继续使用代码(vbYes)或结束脚本(vbNO)。

这是我的代码。

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

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你必须比较“结果”变量,如果是vbYes或vbNo。 下面是一个简单的例子。

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

希望有所帮助。 ;)

答案 1 :(得分:1)

vbYes是一个常量,是名为VbMsgBoxResult的枚举的成员,它定义了一堆相关的常量,包括vbYesvbNo

If vbYes Then

这就像说

If 42 Then

您有一个常量表达式,其值为Long整数,而If语句适用于布尔表达式,其值为Boolean(真/假)。

您需要比较 vbYes某些来获取布尔表达式

If Results = vbYes Then