Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to delete rows after an End (xl Down) without deleting header row?

Posted on 2016-08-10
3
Medium Priority
?
112 Views
Last Modified: 2016-08-12
Hi Guys, I have recorded an Excel macro which puts a Filter in Column 8  for the word "EUR" , then an End (xl down) and then deletes the rows, but I do not want to delete the header row so how do I adapt the code to do this? here's the code:

Sub Macro1()
'
' Macro1 Macro
'

'
    Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$T$298").AutoFilter Field:=8, Criteria1:="EUR"
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.EntireRow.Delete
    Rows("1:1").Select
    Selection.AutoFilter
End Sub
Example.xlsm.xlsx
0
Comment
Question by:JCutcliffe
3 Comments
 
LVL 37

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 41750151
Your code already works, except if row 2 has EUR in it.

change the code from Range("A3").Select to Range("A2").Select
0
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41750164
Try something like this......

Sub DeleteFilteredRows()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
With Rows(1)
    .AutoFilter field:=8, Criteria1:="EUR"
    If Range("A1:A" & lr).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
        Range("A2:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End If
End With
ActiveSheet.AutoFilterMode = 0
End Sub

Open in new window

0
 
LVL 22

Expert Comment

by:Roy Cox
ID: 41750608
This code will prompt the user for a item delete, e.g. EUR. The relevant rows will then be deleted.
Option Explicit

'---------------------------------------------------------------------------------------
' Procedure :   Delete_AutoFiltered_Rows
' Author    :   Roy Cox
' Date      :   13/02/2015
' Purpose   :   Filter Excel Table, delete visible range
'---------------------------------------------------------------------------------------
'
Sub Delete_ListRows_AutoFilter()

    Dim ws As Worksheet
    Dim rng As Range
    Dim strCriteria As String
On Error GoTo err_exit
    '///ask what the use wants to delete
    strCriteria = Application.InputBox("What do you want to delete")
    '/// the sheet containing the Data table
    Set ws = Sheet1

    '///DataBodyRange to Range
    Set rng = ws.Range("A1").CurrentRegion
    On Error GoTo err_exit
    'Filter the Range
    rng.AutoFilter Field:=8, Criteria1:=strCriteria
    '///Delete the visible range
    ws.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
clean_up:
    ws.Range("A1").AutoFilter
    Set ws = Nothing
    On Error GoTo 0
    Exit Sub

err_exit:
    MsgBox "No range was found or the user cancelled"
    Resume clean_up
End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question