Macro search in filtered area

Mihael

Board Regular
Joined
Mar 18, 2016
Messages
170
Hi,

I have a macro which has to search in a filtered area. However when the filter is on, it searches every row, but I don't want that..

Can anyone tell me how the macro can search only the rows which has been filtered?

Code:
Sub Doorlooptijd()
Dim orderNr As String
Dim matNr As String
Dim lRow As Long
Dim lRow2 As Long
Dim myRange As String
Dim myRange2 As String
Dim teller As String
Dim lastRow As String
Dim i As Integer
Dim myRange3 As String
Dim datum1 As String
Dim samenv As String
Dim myRange4 As String
Dim zoek As String
Dim zoek2 As String
Sheets("Doorlooptijd").Activate
lastRow = Cells(Rows.Count, "B").End(xlUp).Row
teller = 4
myRange2 = "E" & teller
myRange3 = "B" & teller
myRange4 = "G" & teller
For i = 1 To lastRow
    myRange2 = "E" & teller
    myRange3 = "B" & teller
    myRange4 = "G" & teller
    Sheets("Doorlooptijd").Activate
    orderNr = Range(myRange3).Value
    Sheets("Bron sheet").Activate
    ActiveSheet.Range("$A$1:$O$28069").AutoFilter Field:=7, Criteria1:=orderNr
    zoek = Range("D" & Rows.Count).End(xlUp).Row
    On Error Resume Next
    zoek = Selection.SpecialCells(xlCellTypeVisible).Copy
    Sheets("test").Activate
    Cells.Select
    ActiveSheet.Paste
    lRow = 0
    On Error Resume Next
    lRow = Application.WorksheetFunction.Match("T140", Range("D:D"), 0)
    On Error GoTo 0
    If lRow = 0 Then
    On Error Resume Next
    lRow = Application.WorksheetFunction.Match("T280", Range("D:D"), 0)
    End If
    
    If lRow > 0 Then
        datum1 = "B" & lRow
        myRange = "D" & lRow
        samenv = datum1 & "," & myRange
        Range(samenv).Copy
        
        Sheets("Doorlooptijd").Activate
        Range(myRange2).Select
        ActiveSheet.Paste
  
    End If
    
    Sheets("test").Activate
    
    On Error Resume Next
    lRow2 = 0
     On Error Resume Next
    lRow2 = Application.WorksheetFunction.Match("V600", Range("D:D"), 1)
    On Error GoTo 0
    If lRow2 = 0 Then
         On Error Resume Next
        lRow2 = Application.WorksheetFunction.Match("V500", Range("D:D"), 1)
    End If
    On Error GoTo 0
    If lRow2 > 0 Then
        datum1 = "B" & lRow2
        myRange = "D" & lRow2
        samenv = datum1 & "," & myRange
        Range(samenv).Copy
        
        Sheets("Doorlooptijd").Activate
        Range(myRange4).Select
        ActiveSheet.Paste
    End If
teller = teller + 1
Sheets("Bron sheet").Activate
ActiveSheet.Range("$A$1:$O$28069").AutoFilter Field:=7
Next i
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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