+ Reply to Thread
Results 1 to 5 of 5

Delete rows based on dropdown list value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Delete rows based on dropdown list value

    Dear Experts,

    Need VBA code guidance for the following requirements and attached excel model sheet for your kind understanding:

    Screenshot (125).png

    If i select the state name in the dropdown list, i want to keep only that states data in the table and rest of the other states data to be deleted and i have used the following code. It works perfectly, but header is deleted as per the below given code. But I want the table with header.

    Sub deleterows_droplist_1()
    Dim ws As Worksheet
    Dim Rng As Range
    Dim lastRow As Long
    
    Set ws = ActiveWorkbook.Sheets("Test")
    lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    Set Rng = ws.Range("A1:A" & lastRow)
    ' filter and delete all but header row
    With Rng
    .AutoFilter Field:=1, Criteria1:="<>" & ws.Range("F1").Value
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    ' turn off the filters
    ws.AutoFilterMode = False
    
    End Sub
    Regards
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Delete rows based on dropdown list value

    Maybe change
    Set Rng = ws.Range("A1:A" & lastRow)
    to
    Set Rng = ws.Range("A2:A" & lastRow)
    Kind regards
    Leo

  3. #3
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Delete rows based on dropdown list value

    Dear Mr. Leo,

    I have tried that too. If i select A2 as a range, that will delete the existing row data. Thanks for your time and reply. Could you please help me in other way?

    Regards

  4. #4
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Delete rows based on dropdown list value

    This can do

    Sub Leo()
    With Sheets("Test")
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:="<>" & .Range("F1").Value
        .Range("A1").CurrentRegion.Offset(1).EntireRow.Delete
        .AutoFilterMode = False
    End With
    End Sub
    Kind regards
    Leo

  5. #5
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Delete rows based on dropdown list value

    Quote Originally Posted by LeoTaxi View Post
    This can do

    Sub Leo()
    With Sheets("Test")
        .Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:="<>" & .Range("F1").Value
        .Range("A1").CurrentRegion.Offset(1).EntireRow.Delete
        .AutoFilterMode = False
    End With
    End Sub
    Kind regards
    Leo
    Dear leo,

    Thanks a lot and it works perfectly. Great learning today.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to delete all rows on all sheets based on list value?
    By mrbusto71 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2020, 09:08 PM
  2. Hide/Unhide rows based on Yes or No answers to questions by dropdown list.
    By Johny1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-12-2015, 06:57 PM
  3. Add/delete rows to other pages based on changes to master list.
    By whiskeycharlie77 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2012, 05:56 PM
  4. Delete Rows Based On A List
    By shopao29 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-17-2012, 11:10 PM
  5. Hide Rows based on dropdown list in another sheet
    By Jeeperman4x4 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-04-2011, 08:01 PM
  6. Delete rows based on list
    By drchris in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-19-2009, 11:47 AM
  7. Delete Rows based on an external list
    By JEFF in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2005, 09:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1