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?
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?
Use the following VBA with the attached to demo. You can adapt to your needs.
Please Login or Register to view this content.
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
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
Apoplogies for the error.
This should work
Please Login or Register to view this content.
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!!!
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, Im not sure what i and lr are. I think its 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 its done.
So I followed what its doing, just not the details of how or all of the syntax. Your help is greatly appreciated.
Thanks for the alternative. I'll try to digest this and revert with questions. Thanks again
Jules
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.
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
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.
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.
What does your current code look like. Provide it so we can analyze it.
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.
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?Please Login or Register to view this content.
Last edited by alansidman; 10-02-2023 at 03:26 PM.
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.
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...
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks