VBA messes up table range when deleting final row

scoobster

New Member
Joined
Apr 27, 2012
Messages
20
Hello.

On 'sheet1' I have a table range named 'PickList' and under the header users are able to add rows. As each row is added a formula is copied to the new row because it's a table range. This is working great.

Via a button on the sheet, a user can delete any rows that has the value 'No' in the 2nd column titled 'Export'. I use 'autofilter' to select the records which meet the criteria and then the code deletes the returned rows. This is working fine.

Where I hit a problem is when it comes to the point where no rows will be left in the table range following the delete. What I get is an empty row under the header row. What I actually want is just the header row to appear so that the user can start adding rows again and these then appear within the table range.

Code is shown below;

Private Sub cmdRemoveItem_Click()
Const FilterString As String = "No"

Dim xval As Variant

Application.ScreenUpdating = False

With worksheets("sheet1").Range("PickList")

.AutoFilter field:=2, Criteria1:=FilterString

Set rngToCheck = worksheets("sheet1").Range("PickList")

For Each xval In .SpecialCells(xlCellTypeVisible)
.EntireRow.Delete
Next xval

.AutoFilter

End With

Application.ScreenUpdating = True

End Sub

Is there a better way to do what I want, or any suggestions on how to change my code so that I get the expected results?

Also, how would I check the number of records returned by the 'autofilter' as I only want the code to run if at least 1 row is returned?

Many thanks for your support.

Paul
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi folks,

Sorry to bump the thread. My head is about to explode as everything I found on this form points to the code I have used but it doesn't work 100% as I expect.

Thanks again for any advice.

Paul
 
Upvote 0
Think I sorted it. As I'm using a range then there must be at least one row after the header. I thought that it should be just the header showing when no data rows were present.
 
Upvote 0

Forum statistics

Threads
1,203,120
Messages
6,053,631
Members
444,674
Latest member
Fieldy1999

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