+ Reply to Thread
Results 1 to 16 of 16

Deleting rows which meet a specific criterion

  1. #1
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Deleting rows which meet a specific criterion

    Sorry if this is simplistic, but...

    Is there a macro command to execute an "if"-function-like check to do something like deleting all rows if and only if a vale is in a column?

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Deleting rows which meet a specific criterion

    Use the following VBA with the attached to demo. You can adapt to your needs.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    Alan:

    Thanks for your help.

    2 things. This Compile Error popped up when I tried to run the macro:

    Attachment 835413


    2) I have specific (though I'm sure you'd say simplistic) questions about what you wrote, but only born from my own ignorance and rust (from not using VBA recently enough). If I posted my questions might you have time to look at them?

    Jules

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Deleting rows which meet a specific criterion

    Apoplogies for the error.

    This should work

    Please Login or Register  to view this content.

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

    Re: Deleting rows which meet a specific criterion

    Another option making use of Alan's sample file...
    Please Login or Register  to view this content.
    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!!!

  6. #6
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    Alan:

    That did work, thanks. I want to be sure I understand what it's doing, so I've posted my version of the narrative of what's happening below. Let me know if I am way off.

    Also, not in the narrative below, but if I also wanted to delete, say the "Genderqueer" entries, would I duplicate a particular command and change the crit? if so which lines?

    1) Option Explicit

    2) Sub DelX()
    3) Dim i As Long, lr As Long
    4) Dim crit As String
    5) Application.ScreenUpdating = False
    6) lr = Range("A" & Rows.Count).End(xlUp).Row
    7) crit = "Female"
    8) For i = lr To 2 Step -1
    9) If Range("E" & i) = crit Then
    10) Range("E" & i).EntireRow.Delete
    11) End If
    12) Next i
    13) Application.ScreenUpdating = True
    14) MsgBox "Action Completed"

    15) End Sub

    Lines 1) and 2) are obviously names. Lines 3 and 4 contain the Dim command (from dimension), but are the parts after those Name Definitions or a separate command? What are the “i” and “lr” and “Long”?
    Line 5 freezes the screen while running.

    Line 6 This seems to be defining “lr” as a range (which I think says it is column “A” and the number of rows is defined by the “Rows.Count” command?) End(xlUp) is the command for Shift+End+Up to get to cove the full range or a blank or the top, right? What does the.’Row’ after the period at the end do?

    Line 7 I think crit = “Female” sets the criterion to Female,

    Line 8 is setting up the repetitive if/then statement, but, again, I’m not sure what “i” and “lr” are. I think it’s specifying how many times to repeat the action, and that Step -1 is either a part of that, or an additional command, but if you could briefly walk me through that it would help.

    Line 9 is saying that if Column E and “i” match the crit (criteria, I assume?), then [execute] Line 10, delete the entire row. Line 11 ends the imbedded If, and Line 12 ends the For Statement.

    Line 13 turns the screen back on, and Line 14 posts a message telling you it’s done.

    So I followed what it’s doing, just not the details of how or all of the syntax. Your help is greatly appreciated.

  7. #7
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    Thanks for the alternative. I'll try to digest this and revert with questions. Thanks again

    Jules

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Deleting rows which meet a specific criterion

    i is a variable that allows the VBA to loop form the last row to the second row. The -1 tells it to loop form the last record to the second line. lr is a variable to find the last line in the column. We start at the bottom and work our way to the top because if we started at the top and worked our way to the bottom, it would not perform correctly. To test this and see, change the loop to start with i and end with lr on the sample to show why you need to start at the bottom.

  9. #9
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    sintek:

    Thanks, that code also worked. I will try to work through the commands in an effort to decipher the steps in my brain, but I appreciate you effort. Marking post as solved and bumping your "Rep".

    Jules

  10. #10
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    This was very helpful, thanks! I was able to figure out which part to duplicate to delete more than one criteria, so I was able to apply it to the project on which I was stuck. Bumping you "Rep" again, and making the thread solved.

  11. #11
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    Thanks again for this help. I thought I understood, but I tried to just replicate the part of your macro, changing the Const Crit = "Female" part, but in trying to replicate and change the Const Crit part to delete a second entry in the same column I got a "Compile Error: Duplicate Declaration in Current Scope" error. Shouldn't I just be able to do the same instruction set with a different variable? Any help would be appreciated.

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Deleting rows which meet a specific criterion

    What does your current code look like. Provide it so we can analyze it.

  13. #13
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    This is the alteration of what you provided:
    Please Login or Register  to view this content.

    I tried to do a second criteria by duplicating your instrux as such

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    This gave me the "Compile Error: Duplicate Declaration in Current Scope" error. I am sure that my assumption about the syntax working with a simple replication (and change of one variable) is the problem. What is the proper way to write the code to allow a second set of row deletions?
    Last edited by alansidman; 10-02-2023 at 03:26 PM.

  14. #14
    Registered User
    Join Date
    01-13-2010
    Location
    Florida
    MS-Off Ver
    Office 2021
    Posts
    20

    Re: Deleting rows which meet a specific criterion

    One other thing that occurred to me would be to have your instrux with different letters ("W", "L", "P") in separate macros and invoke them within the original.

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

    Re: Deleting rows which meet a specific criterion

    Why don't you just filter on that 3 criteria and then delete the filtered rows...
    Also, upload a sample file that one can make use of...

  16. #16
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Deleting rows which meet a specific criterion

    Code Tags Added
    Your post does not comply with our General Guide lines. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html

    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

    However, if you continue to not use code tags, you can expect to have your thread BLOCKED until you add them yourself.

+ 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. Replies: 8
    Last Post: 04-19-2018, 02:11 AM
  2. Copying Rows that meet criteria/criterion
    By protoking17 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-06-2013, 09:27 AM
  3. Populate ComboBox with rows that meet criterion
    By val64 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-05-2011, 12:46 PM
  4. Copying rows which meet certain criterion into another workbook
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-12-2010, 04:45 PM
  5. Deleting Rows NOT EQUAL to multiple criterion
    By ballgameaddict in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-29-2008, 02:25 PM
  6. Replies: 7
    Last Post: 06-26-2007, 11:33 AM
  7. [SOLVED] Delete rows that don't meet specific criterion
    By SITCFanTN in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-04-2006, 10:25 AM

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