Advance Filter Last Row

mdavontuur25

New Member
Joined
May 5, 2011
Messages
11
Hi Excel Gurus

I have been struggling to write this code for weeks.

I want to use advance filter in a macro to extract data in a worksheet that starts with cell B8 (headings is in this row) and it must find the last row of data. eg range = B8:last data row

Currently I have this code

Sub Filter()
Sheets("cbpay").Range("B8:M26").End(xlUp).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("A1:G1"), Unique:=False
End Sub

As you can see the range is currently just from B8:M26, however sometimes I have 2000 rows and at other times only 4 or 5.

Please help!!!
mOMAn
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try

Code:
Sub Filter()
Dim LR As Long
With Sheets("cbpay")
    LR = .Range("B" & Rows.Count).End(xlUp).Row
    .Range("B8:M" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("A1:G1"), Unique:=False
End With
End Sub
 
Upvote 0
Hi,

Is this what you are looking for?

Code:
Sub Filter()
    Sheets("cbpay").Range("B8:M" & Range("M" & Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=Range("A1:G1"), Unique:=False
End Sub
 
Upvote 0
Hi Guys

Thank you for your spedily response how ever on both macros I get errors. John on yours it says missing or illegal field name, and Peter on yours it hi lights LR=... in yellow.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,704
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