Auto filter and delete blank rows

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help?

On sheet1 I have data
A1:G1 have headers A B C D E F G
A2:H29 has data

I have recored a macro and added (lRow) to the code to delete blank rows and unwanted data using AutoFilter. I want to Select ("A2:G" & lRow) at the end of the code which should select "A2:G11" (the filtered data) but for some reason it selects A2:G29

Can anyone explain why it does this and how I can fix it?

This is my code and worksheet example any help would be apprieciated

Regards

pwill

Code:
Sub FilterData()

    Dim wsSht As Worksheet: Set wsSht = Sheet1
    Dim lRow As Long
    
    lRow = wsSht.Cells(Rows.Count, "A").End(xlUp).Row

    Range("G2:G" & lRow).Select
    Selection.Delete Shift:=xlToLeft
    Range("A1:G" & lRow).AutoFilter
    Range("A2:G" & lRow).AutoFilter Field:=7, Criteria1:="="
    Range("A3:G" & lRow).EntireRow.Delete
    Selection.AutoFilter
    
    Range("A2:G" & lRow).Select
    
End Sub

eg


ABCDEFGHIJK
1ABCDEFG



2123456
7


3










4 01/01/18









5123456
7


6










7 01/01/18









8123456
7


9










10 01/01/18









11123456
7


12










13 01/01/18









14123456
7


15










16 01/01/18









17123456
7


18










19 01/01/18









20123456
7


21










22 01/01/18









23123456
7


24










25 01/01/18









26123456
7


27










28 01/01/18









29123456
7



<colgroup><col style="width:48pt" span="3" width="64"> <col style="width:48pt" span="5" width="64"> <col style="width:48pt" span="4" width="64"> </colgroup><tbody>
</tbody>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
It does that because you haven't reset the value of lRow after deleting the blank rows
 
Upvote 0
The same way you set it initially ;)
 
Upvote 0
Do you mean add another lastRow to the code?

eg
Code:
Sub FilterData()

    Dim wsSht As Worksheet: Set wsSht = Sheet1
    Dim lRow As Long
    Dim lastRow As Long
    
    lRow = wsSht.Cells(Rows.Count, "A").End(xlUp).Row
    lastRow = wsSht.Cells(Rows.Count, "A").End(xlUp).Row
 
Last edited:
Upvote 0
No, you can't declare a variable more than once.
You just need to change the value it holds, so add this line
Code:
lRow = wsSht.Cells(Rows.Count, "A").End(xlUp).Row
just after you remove the autofilter
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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