Solved

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

Posted on 2016-08-10
3
90 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 35

Accepted Solution

by:
Kimputer earned 500 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 30

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 19

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

763 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