How to add one more filter to this VBA code?

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
To this VBA code, I need to add one more filter, to choose from the data in Column 14, but I would like to be able to do it from a MsgBox. Is it possible?
Rich (BB code):
Sub InformeDesplazmientoEspaña()
    On Error Resume Next
    m = MsgBox("¿QUIERE REALIZAR EL INFORME?", vbQuestion + vbYesNoCancel, "INFORME")
    If m = vbCancel Then Exit Sub
    Application.ScreenUpdating = False
    Sheets("INFORMES").Unprotect ("123")
    Sheets("BDATOS").Unprotect ("123")
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"
    With Sheets("INFORMES")
        .Range("A2:L65536") = ""
        Sheets("BDATOS").Range("A2:L" & Sheets("BDATOS").Range("a65536").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Destination:=.Range("A2")
        .Cells.FormatConditions.Delete
        .Range("A2:L" & .Range("a65536").End(xlUp).Row).Interior.ColorIndex = xlNone
    End With
    Sheets("BDATOS").Range("b1").AutoFilter = False
    Sheets("BDATOS").Protect ("123")
    'Application.ScreenUpdating = True
    Sheets("INFORMES").Select
    Ajustar
    Range("A1").Select
    Sheets("INFORMES").Protect ("123")
    MsgBox "INFORME REALIZADO", vbInformation + vbOKOnly, "INFORME"
    Exit Sub
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I would go for an input box rather than a message box. See whether this works for you.

Replace this line:
VBA Code:
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"

with this snippet
VBA Code:
    Dim Crit As String
    Crit = VBA.InputBox("Enter single criterion for column N", "AutoFilter")
    
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"
    If Len(Crit) > 0 Then
        On Error Resume Next
        Sheets("BDATOS").Range("A1").AutoFilter Field:=14, Criteria1:=Crit
        If Err.Number > 0 Then
            MsgBox "Column N was not filtered" & vbNewLine & _
                   "(Error " & Err.Number & ": " & Err.Description & ")", vbExclamation, "Error on AutoFilter"
            Err.Clear
        End If
        On Error GoTo 0
    End If
 
Upvote 0
I would go for an input box rather than a message box. See whether this works for you.

Replace this line:
VBA Code:
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"

with this snippet
VBA Code:
    Dim Crit As String
    Crit = VBA.InputBox("Enter single criterion for column N", "AutoFilter")
   
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"
    If Len(Crit) > 0 Then
        On Error Resume Next
        Sheets("BDATOS").Range("A1").AutoFilter Field:=14, Criteria1:=Crit
        If Err.Number > 0 Then
            MsgBox "Column N was not filtered" & vbNewLine & _
                   "(Error " & Err.Number & ": " & Err.Description & ")", vbExclamation, "Error on AutoFilter"
            Err.Clear
        End If
        On Error GoTo 0
    End If
It throws error in the line of code that I mark you in red
Rich (BB code):
Sub InformeDesplazmientoEspaña()
    On Error Resume Next
    m = MsgBox("¿QUIERE REALIZAR EL INFORME?", vbQuestion + vbYesNoCancel, "INFORME")
    If m = vbCancel Then Exit Sub
    Application.ScreenUpdating = False
    Sheets("INFORMES").Unprotect ("acuario3511")
    Sheets("BDATOS").Unprotect ("acuario3511")
        Dim Crit As String
    Crit = VBA.InputBox("Enter single criterion for column N", "AutoFilter")
    
    Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:="España"
    If Len(Crit) > 0 Then
        On Error Resume Next
        Sheets("BDATOS").Range("A1").AutoFilter Field:=14, Criteria1:=Crit
        If Err.Number > 0 Then
            MsgBox "Column N was not filtered" & vbNewLine & _
                   "(Error " & Err.Number & ": " & Err.Description & ")", vbExclamation, "Error on AutoFilter"
            Err.Clear
        End If
        On Error GoTo 0
    End If
    With Sheets("INFORMES")
        .Range("A2:L65536") = ""
        Sheets("BDATOS").Range("A2:L" & Sheets("BDATOS").Range("a65536").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy _
        Destination:=.Range("A2")
        .Cells.FormatConditions.Delete
        .Range("A2:L" & .Range("a65536").End(xlUp).Row).Interior.ColorIndex = xlNone
    End With
    Sheets("BDATOS").Range("b1").AutoFilter = False ' ERROR
    Sheets("BDATOS").Protect ("acuario3511")
    'Application.ScreenUpdating = True
    Sheets("INFORMES").Select
    Ajustar
    Range("A1").Select
    Sheets("INFORMES").Protect ("acuario3511")
    MsgBox "INFORME REALIZADO", vbInformation + vbOKOnly, "INFORME"
    Exit Sub
End Sub
 
Upvote 0
I see, try changing this

VBA Code:
Sheets("BDATOS").Range("b1").AutoFilter = False ' ERROR

into this
VBA Code:
Sheets("BDATOS").AutoFilterMode = False
 
Upvote 0
Solution
I see, try changing this

VBA Code:
Sheets("BDATOS").Range("b1").AutoFilter = False ' ERROR

into this
VBA Code:
Sheets("BDATOS").AutoFilterMode = False
Now it works correctly. Is it possible to do the same with the Commune 7 filter, to choose it also through a MsgBox, in the same VBA code?
 
Upvote 0
You are welcome and thanks for the feedback (y)

Is it possible to do the same with the Commune 7 filter, to choose it also through a MsgBox, in the same VBA code?
Yes, just copy the code of my post #2, declare an extra variable and change some arguments of the AutoFilter method accordingly:

VBA Code:
    Dim Crit7 As String, Crit14 As String
    
    Crit7 = VBA.InputBox("Enter single criterion for column H", "AutoFilter")
    Crit14 = VBA.InputBox("Enter single criterion for column N", "AutoFilter")
    
    If Len(Crit7) > 0 Then
        On Error Resume Next
        Sheets("BDATOS").Range("A1").AutoFilter Field:=7, Criteria1:=Crit7
        If Err.Number > 0 Then
            MsgBox "Column H was not filtered" & vbNewLine & _
                   "(Error " & Err.Number & ": " & Err.Description & ")", vbExclamation, "Error on AutoFilter"
            Err.Clear
        End If
        On Error GoTo 0
    End If
    
    If Len(Crit14) > 0 Then
        On Error Resume Next
        Sheets("BDATOS").Range("A1").AutoFilter Field:=14, Criteria1:=Crit14
        If Err.Number > 0 Then
            MsgBox "Column N was not filtered" & vbNewLine & _
                   "(Error " & Err.Number & ": " & Err.Description & ")", vbExclamation, "Error on AutoFilter"
            Err.Clear
        End If
        On Error GoTo 0
    End If
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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