Yes/No answer from pop-up

pleasehelpthischick

New Member
Joined
Mar 1, 2023
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet with a ton of data that I need to filter based on questions the user needs to answer. I already have a pop-up working that asks a yes or no question. If the answer is yes, it filters the right data and we are good. However, if the answer is no, I need another question to pop-up which asks them to select high, medium, or low. Columns H, I, J are high, medium, low respectively. Based on each selection, I will need those columns filtered.

Example:
User hits a button and it asks "is this important?"
User hits yes, everything filters fine.
User hits no, then should be asked "is this high, medium, low?"
If the user selects high, column H will filter to only show the value of High that is in the cell. (The rows only contain either blank or high)
If the user selects low, column I will filter to only show the value of Low that is in the cell. (The rows only contain either blank or low)
The same for the medium option.

Only one selection can be made, high medium or low.

This is the code I have so far that is working:

Sub User_Question()
Dim answer As Integer

answer = MsgBox("Is this system important?", vbQuestion + vbYesNo + vbDefaultButton2, "")

If answer = vbYes Then
ActiveSheet.ListObjects("Table22").Range.AutoFilter Field:=1, Criteria1:= _
"Yes"
Else
ActiveSheet.ListObjects("Table22").Range.AutoFilter Field:=1, Criteria1:= _
"No"

End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This should incorporate everything that you want to do.

Always state the worksheet that you are working on.

VBA Code:
Public Sub subPromptToFilter()
Dim strMsg As String
Dim varAnswer As Variant
Dim blnAnswerValid As Boolean
Dim arrCriteria() As Variant
Dim answer As Integer

    On Error Resume Next
    Worksheets("Prompt").ShowAllData
    On Error GoTo 0
    ActiveWindow.ScrollRow = 1
    ActiveWindow.ScrollColumn = 1

    If MsgBox("Is this system important?", vbQuestion + vbYesNo + vbDefaultButton2, "") = vbYes Then
    
        Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=1, Criteria1:="Yes"
        
        MsgBox "Yes"
        
        Exit Sub
        
    End If
    
    strMsg = "1 : High" & vbCrLf & _
                      "2 : Medium" & vbCrLf & _
                      "3: Low" & vbCrLf & _
                      "4 : Show All Rows"
    
    arrCriteria = Array("High", "Medium", "Low")
    
    blnAnswerValid = False
    
    Do While Not blnAnswerValid
    
        varAnswer = InputBox(strMsg, "Enter 1, 2 or 3")
               
        If StrPtr(varAnswer) = 0 Then
            
            MsgBox "User cancelled.", vbInformation, "Warning!"
            
            ' User cancelled. Code here.
            
            blnAnswerValid = True
        
        ElseIf varAnswer = vbNullString Then
            
            ' User did not enter anything. Ask again.
        
        Else
        
            If InStr(1, "1,2,3,4", Trim(varAnswer), vbTextCompare) > 0 Then
                
                On Error Resume Next
                Worksheets("Prompt").ShowAllData
                On Error GoTo 0
                
                ActiveWindow.ScrollRow = 1
                
                ActiveWindow.ScrollColumn = 1
                
                If Val(varAnswer) < 4 Then
                    Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=Val(varAnswer) + 7, Criteria1:=arrCriteria(Val(varAnswer) - 1)
                End If
                
            Else
        
                blnAnswerValid = False
            
            End If
        
        End If
    
    Loop

End Sub
 
Upvote 0
When I run it and click yes I get a debug error on this line:

Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=1, Criteria1:="Yes"

If I select no, the box comes up to choose high, med, or low but it also says show all rows, which i dont want. If I select 1,2, or 3, I get a debug error on this line:

Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=val(varAnswer) + 7, Criteria1:=arrCriteria(val(varAnswer) - 1)

Thank you!
 
Upvote 0
You need to state the worksheet that you are on.

Where it says Worksheets("Prompt") you need to change it to make reference to the sheet that you are on.

It is not good practice to use ActiveSheet.

This is for you to build upon, not necessarily the finished code.

VBA Code:
Public Sub subPromptToFilter()
Dim strMsg As String
Dim varAnswer As Variant
Dim blnAnswerValid As Boolean
Dim arrCriteria() As Variant
Dim answer As Integer

    On Error Resume Next
    Worksheets("Prompt").ShowAllData
    On Error GoTo 0
    ActiveWindow.ScrollRow = 1
    ActiveWindow.ScrollColumn = 1

    If MsgBox("Is this system important?", vbQuestion + vbYesNo + vbDefaultButton2, "") = vbYes Then
    
        Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=1, Criteria1:="Yes"
                
        Exit Sub
        
    End If
    
    strMsg = "1 : High" & vbCrLf & _
                      "2 : Medium" & vbCrLf & _
                      "3: Low"
    
    arrCriteria = Array("High", "Medium", "Low")
    
    blnAnswerValid = False
    
    Do While Not blnAnswerValid
    
        varAnswer = InputBox(strMsg, "Enter 1, 2 or 3")
               
        If StrPtr(varAnswer) = 0 Then
            
            MsgBox "User cancelled.", vbInformation, "Warning!"
            
            ' User cancelled. Code here.
            
            blnAnswerValid = True
        
        ElseIf varAnswer = vbNullString Then
            
            ' User did not enter anything. Ask again.
        
        Else
        
            If InStr(1, "1,2,3", Trim(varAnswer), vbTextCompare) > 0 Then
                
                On Error Resume Next
                Worksheets("Prompt").ShowAllData
                On Error GoTo 0
                
                ActiveWindow.ScrollRow = 1
                
                ActiveWindow.ScrollColumn = 1
        
                Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=Val(varAnswer) + 7, Criteria1:=arrCriteria(Val(varAnswer) - 1)
                
            Else
        
                blnAnswerValid = False
            
            End If
        
        End If
    
    Loop

End Sub
 
Upvote 0
OK, I made the updates you suggested but when I select high, med, or low, the filter applies but it's not showing the rows, they appear to be hidden. Also, checking the filter, nothing is checked. And the selection box keeps coming up after making a choice.
 
Upvote 0
Have you already got a filter applied before you run the code?

If you cancel what do you see row wise?

Are you using H,I and J for the filter columns?

Add in this line :

blnAnswerValid = True

beneath this line:

Worksheets("Prompt").ListObjects("Table22").Range.AutoFilter Field:=Val(varAnswer) + 7, Criteria1:=arrCriteria(Val(varAnswer) - 1)
 
Upvote 0
Solution
The box goes away now after answering, so that is fixed. But it's still not filtering properly.
If I select 1 for High, this is what it does. It's put a filter on column I (should be column H) but then nothing is actually filtered but rows are hidden?

1677869696611.png
 
Upvote 0
Hello, nevermind on this. I got it working! I had a typo in the column name for medium, I made those changes and everything runs perfect. Thank you so much for your help!
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top