VBA: Filter range by dropdown list selection + another criteria

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Morning all,

I hope you all had a great weekend.

This newly created thread follows this one.

I wanted to filter the range D4:D590 by value selected from dropdown list located at the cell DK1, which has been solved by Fluff and fadee2. I used this code for it:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "DK1" Then
      Range("D4:D590").AutoFilter 1, Target.Value
   End If
End Sub

Now, I would like to add one more criteria.

So basically when I select a value from dropdown list located at cell DK1 it will filter range D4:D590 based on DK1 cell selection and I would like to hide all zero and blank values in the range DO4:DO590 with it.

Thank you.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
if by
hide all zero and blank values
you mean you want to further filter the data to omit zero and blank values then try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "DK1" Then
      Range("D4:D590").AutoFilter 1, Target.Value
      Range("DO4:DO590").AutoFilter 119, "<> 0", xlAnd, "<>" & ""
   End If
End Sub

hth
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "DK1" Then
      Range("D4:DO590").AutoFilter 1, Target.Value
      Range("D4:DO590").AutoFilter 112, "<>0", xlAnd, "<>"
   End If
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "DK1" Then
      Range("D4:DO590").AutoFilter 1, Target.Value
      Range("D4:DO590").AutoFilter 112, "<>0", xlAnd, "<>"
   End If
End Sub
First range should be D4:D590, second range should be DO4:DO590. But still, I am getting a run time error 1004.

VBA Code:
      Range("DO3:DO589").AutoFilter 112, "<>0", xlAnd, "<>"
this line is highlighted by yellow.
 
Upvote 0
Remove any filters you have in place & try it again.
 
Upvote 0
Removed all filters, but still gettin that same error.
 
Upvote 0
Did you change the ranges in the code I provided?
 
Upvote 0
I have changed ranges to this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "DK1" Then
      Range("D4:D590").AutoFilter 1, Target.Value
      Range("DO4:DO590").AutoFilter 112, "<>0", xlAnd, "<>"
   End If
End Sub
 
Upvote 0
In that case change them back to what I suggested. You can only have one autofilter on a sheet unless you are using structured tables.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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