+ Reply to Thread
Results 1 to 18 of 18

Delete Rows based on Criteria

  1. #1
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Delete Rows based on Criteria

    i have a large set of data when filtered looks like this;

    RESOURCE STATUS
    User 1 Inactive
    User 1 Active
    User 2 Inactive
    User 2 Inactive
    User 2 Active
    User 3 Inactive
    User 3 Active

    i found this below code and tweaked it but nothing is happening

    Please Login or Register  to view this content.
    Thank you.

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Delete Rows based on Criteria

    NON VBA solution

    C2 =If(B2="inactive","delete","") and drag down.

    After that, filter on column C on delete.

    Delete the filtered rows.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Re: Delete Rows based on Criteria

    sorry i forgot to mention that some of the INACTIVE data should remain as we use it for looking up their status..
    main objective here is remove all inactive status data for each of the users.
    if a resource has 3 records and it all says inactive the code should move past it and go to the next person.

    RESOURCE STATUS
    User 1 Inactive
    User 1 Active

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Delete Rows based on Criteria

    HOW TO ATTACH YOUR SAMPLE WORKBOOK:

    Fast answers need clear examples. Post a small Excel sheet (not a picture) showing realistic & representative sample data WITHOUT confidential information (10-20 rows, not thousands...) and some manually calculated results. Just before posting, scroll down to GO ADVANCED, click, and then scroll down to MANAGE ATTACHMENTS and click again. Now follow the instructions at the top of that screen.

  5. #5
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    "
    RESOURCE STATUS
    User 1 Inactive
    User 1 Active
    User 2 Inactive
    User 2 Inactive
    User 2 Active
    User 3 Inactive
    User 3 Active
    User 4 Active
    User 4 Active
    User 5 Inactive
    User 5 Inactive

    "

    In your example (With a couple of additional records), which records should the macro delete?

  6. #6
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12
    Quote Originally Posted by Dante Amor View Post
    "
    RESOURCE STATUS
    User 1 Inactive
    User 1 Active
    User 2 Inactive
    User 2 Inactive
    User 2 Active
    User 3 Inactive
    User 3 Active
    User 4 Active
    User 4 Active
    User 5 Inactive
    User 5 Inactive

    "

    In your example (With a couple of additional records), which records should the macro delete?
    Inactive records on top of active records for each different user. Based on this example its lines 1,3,4 & 6

  7. #7
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    It's a bit longer macro, but I guess it's pretty quick. It all depends on the number of records on your sheet and if you have formulas.


    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Re: Delete Rows based on Criteria

    Quote Originally Posted by Dante Amor View Post
    It's a bit longer macro, but I guess it's pretty quick. It all depends on the number of records on your sheet and if you have formulas.


    Please Login or Register  to view this content.
    Thanks Dante. it works on a non-tabled data. my current one im planning in using this is on a table i need to refresh everyday.
    tried using it but its showing me an error on the last row code

    rng.EntireRow.Delete

    i tried tweaking it by adding this but it converted to a range

    Dim TableRange As ListObject


    For Each TableRange In ActiveSheet.ListObjects
    TableRange.Unlist
    Next TableRange

  9. #9
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    In your first post you did not mention that you had a table, nor could I appreciate it in your example, nor does your code work with a table.
    To adapt the code, you could mention important data such as:
    - I have a table.
    - Name of the table.
    - All the names of the fields in the table.
    - The names of the fields with which you are going to work.
    - An example of the data.
    - An example of the result you want.
    - Name of the sheet.
    - Initial range of the table, row column.
    - Final table range, row column.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,236

    Re: Delete Rows based on Criteria

    i found this below code and tweaked it but nothing is happening
    What are you wanting to achieve...Upload a sample file as requested in Post 4 and explain in detail what you require...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  11. #11
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Re: Delete Rows based on Criteria

    i uploaded the sample file. it already has 2 moduules. module 1 finds the duplicate records, highlights and sort them alphabetically.
    upon running that 1st module the structure of the data is like this
    Annotation 2020-07-29 053349.jpg

    what i want to happen
    a. if the resource is a duplicate and one record says inactive, delete that inactive row and keep the active one
    b. if the resource is a duplicate and has two inactive records and 1 active, delete the inactive row and keep the active one
    c. if the resource is a duplicate and both records are inactive, do nothing and skip to the next duplicate record.
    d. Keep it as a table. It is connected to a sharepoint list.
    Attached Files Attached Files
    Last edited by alfaromeo30; 07-28-2020 at 11:14 PM.

  12. #12
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    - I have a table.
    - Name of the table.
    - All the names of the fields in the table.
    - The names of the fields with which you are going to work.
    - An example of the data.
    - An example of the result you want.
    - Name of the sheet.
    - Initial range of the table, row column.
    - Final table range, row column.

    You did not answer my questions, it is difficult to assume all the data, but try the following. At the end I put the name of the table: "Table1", you can adjust it to your data.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12
    Quote Originally Posted by sintek View Post
    What are you wanting to achieve...Upload a sample file as requested in Post 4 and explain in detail what you require...
    Uploaded the sample file and things i want to achieve.

  14. #14
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Re: Delete Rows based on Criteria

    Thanks Dante. Is there a way to keep the table and not convert them to range? this table is connected to a sharepoint list..i wanted to just refresh it to get new data.

  15. #15
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,236

    Re: Delete Rows based on Criteria

    As it is already Sorted...Try this...
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    Please Login or Register  to view this content.
    Forgive the confusion. I thought you wanted to convert the table into range, because of your code in post #8.

    Now you know that you must report if your data is in a range or in a table.

    Try the following:


    Please Login or Register  to view this content.
    Last edited by Dante Amor; 07-29-2020 at 08:14 AM.

  17. #17
    Registered User
    Join Date
    08-06-2015
    Location
    Philippines
    MS-Off Ver
    2013
    Posts
    12

    Re: Delete Rows based on Criteria

    thanks Dante. this works.

  18. #18
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Delete Rows based on Criteria

    I'm glad to help you. Thanks for the feedback.

+ 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. [SOLVED] Delete rows based on certain criteria
    By myobreportguru in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2018, 06:44 AM
  2. Delete rows based on criteria? help
    By jsabo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2012, 10:34 PM
  3. Delete Rows based on Criteria
    By amandaca in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-19-2011, 09:40 AM
  4. delete rows based on a criteria
    By Ralem in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2011, 06:47 AM
  5. [SOLVED] Delete rows based on criteria
    By Chris_t_2k5 in forum Excel General
    Replies: 2
    Last Post: 04-11-2006, 08:52 AM
  6. Delete rows based on certain criteria
    By Coal Miner in forum Excel General
    Replies: 2
    Last Post: 03-03-2006, 02:00 PM

Tags for this Thread

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