Filter when name not found goes on a loop

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi good morning, I have the code that works great apart when the name cant filter on the name I M2, it goes on a loop about times, until it finally stops and highlights empty fields, is there anyway t stop the loop and return nothing straight away instead?

Code:
Private Sub dofilter()
    Sheets("Filter").Select
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Clear
    
    Sheets("Work Issue").Select
    Range("C:C").AutoFilter Field:=3, Criteria1:=Range("M2").Value
    Range("A1").Select
    
    ActiveCell.Offset(1).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    Sheets("Filter").Select
    ActiveSheet.Paste Destination:=Range("C10")
     
    Columns.AutoFit
    Range("B2").Select
End Sub
 
Hi when I press esc when it goes in the loop the code below is highlighted in yellow. does this mean anything?

Code:
.ReadingOrder = xlContext

This belongs to the code below on the button

Code:
Private Sub FilterData_Click()
    Sheets("Filter").Select
    Range("b2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Clear
    
    Sheets("Work Issue").Select
    Range("C:C").AutoFilter Field:=3, Criteria1:=Range("M2").Value
    Range("A1").Select
    
    ActiveCell.Offset(1).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Filter").Select
    
    Range("b2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
            Range("b2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
        With Selection
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
 
    Range("B2").Select
 End With
 
End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Dave, now removed, thankyou for that.

Sorry I didn't understand exactly where to put you code? if you can advise what to do please, I am still learning. thanks for the help


Hi,
Place my suggested code in a standard module (ensure other code(s) with same name are removed)

Then update your userform command button code as follows

Code:
Private Sub FilterData_Click()
    Dofilter
End Sub

Dave
 
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