VBA autofilter question

McCarls

New Member
Joined
Jun 13, 2011
Messages
2
For a project I'm doing I have a userform that autofilters a range of products according to values of particular comboboxes within the form. Once the autofilter is complete, the resulting rows are copied to a separate worksheet, "temp3".

Problem: For the most part this code works well, however, if the autofilter does not yield any results i.e. no products match the user specs, the entire product list is copied over instead of simply copying no cells.

Here is the code:
Code:
Private Sub ComboBox1_Change()
ComboBox2.Value = "Any"
With ActiveWorkbook.Worksheets("ODU")
.Range("A1:I1").AutoFilter
If ComboBox1 = "Any" Then
.Range("A1:I1").AutoFilter field:=5, Operator:=xlAnd
Else
.Range("A1:I1").AutoFilter field:=5, Criteria1:=CInt(ComboBox1), Operator:=xlAnd
End If


If ComboBox2 = "Any" Then
.Range("A1:I1").AutoFilter field:=8, Operator:=xlAnd
Else
.Range("A1:I1").AutoFilter field:=8, Criteria1:=CInt(ComboBox2), Operator:=xlAnd
End If

If CheckBox1 Then
.Range("A1:I1").AutoFilter field:=6, Criteria1:="TRUE"
Else
.Range("A1:I1").AutoFilter field:=6, Criteria1:="FALSE"
End If
   Worksheets("temp2").Cells.Clear
   Set Rng = .AutoFilter.Range
   Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1).Copy _
     Destination:=Worksheets("temp2").Range("A1")
End With

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi
check the last three lines of code.

Points noted:
You set the filtered range and then offset it by 1,0. This must be copying things out of the filtered range.
check you resize argument, what is the value for the column argument, i thing it will defalt to o meaning the same as it was.

Hope this help
 
Upvote 0
I found a solution to the problem. Basically I added a .SpecialCells(xlCellTypeVisible) so it only grabs the visible cells from the range


Code:
On Error Resume Next
Worksheets("temp2").Cells.Clear   
Set Rng = .AutoFilter.Range
Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy _
     Destination:=Worksheets("temp2").Range("A1")
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,687
Members
452,938
Latest member
babeneker

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