Delete EEE Parts from Excel File

CC268

Active Member
Joined
Mar 7, 2016
Messages
328
Hello,

I'd like to come up with some VBA Code that will look up the following keywords in Columns E and I of my Excel file and delete those rows:

  • Jans (this keyword needs to be searched in Column G only)
  • Resistor
  • %
  • Diode
  • Trans
  • MCKT
  • Micro
  • Conn
  • Relay
  • Inductor

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This assumes the key words, if present, are not part of a longer string and that you don't care about case sensitivity. Both of these limitations are easily eliminated if desired.
Code:
Sub CC268()
Dim Wrds As Variant, Gwrd As String
Gwrd = "Jans"
Wrds = Array("Resistor", "%", "Diode", "Trans", "MCKT", "Micro", "Conn", "Relay", "Inductor")
Range("G:G").Replace Gwrd, "#N/A", xlWhole, , False
Application.ScreenUpdating = False
For i = LBound(Wrds) To UBound(Wrds)
    Range("E:E").Replace Wrds(i), "#N/A", xlWhole, , False
    Range("I:I").Replace Wrds(i), "#N/A", xlWhole, , False
Next i
Range("E:I").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This assumes the key words, if present, are not part of a longer string and that you don't care about case sensitivity. Both of these limitations are easily eliminated if desired.
Code:
Sub CC268()
Dim Wrds As Variant, Gwrd As String
Gwrd = "Jans"
Wrds = Array("Resistor", "%", "Diode", "Trans", "MCKT", "Micro", "Conn", "Relay", "Inductor")
Range("G:G").Replace Gwrd, "#N/A", xlWhole, , False
Application.ScreenUpdating = False
For i = LBound(Wrds) To UBound(Wrds)
    Range("E:E").Replace Wrds(i), "#N/A", xlWhole, , False
    Range("I:I").Replace Wrds(i), "#N/A", xlWhole, , False
Next i
Range("E:I").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
Application.ScreenUpdating = True
End Sub

Ahh good point. I don't care about case sensitivity, but these key words will almost always be part of a longer string.

Example: "Conn" may be "Connector, Plug", etc...

How would I modify the code to accommodate that?
 
Last edited:
Upvote 0
Ahh good point. I don't care about case sensitivity, but these key words will almost always be part of a longer string.

Example: "Conn" may be "Connector, Plug", etc...
Then change "xlWhole" to "xlPart" in the 3 lines where it appears. Be aware that this may cause some deletions you were not expecting because the keyword is embedded in a longer word. If you want to run a "nondestructive" test first, change "Delete" to "Select" in the next to last line before "End Sub". Then look carefully at all the selected rows to see if you would want them all deleted. Otherwise, once you run the macro there's no undo and deleted rows will be gone.
 
Upvote 0
Then change "xlWhole" to "xlPart" in the 3 lines where it appears. Be aware that this may cause some deletions you were not expecting because the keyword is embedded in a longer word. If you want to run a "nondestructive" test first, change "Delete" to "Select" in the next to last line before "End Sub". Then look carefully at all the selected rows to see if you would want them all deleted. Otherwise, once you run the macro there's no undo and deleted rows will be gone.

Thanks! I will give it a shot.
 
Upvote 0
Then change "xlWhole" to "xlPart" in the 3 lines where it appears. Be aware that this may cause some deletions you were not expecting because the keyword is embedded in a longer word. If you want to run a "nondestructive" test first, change "Delete" to "Select" in the next to last line before "End Sub". Then look carefully at all the selected rows to see if you would want them all deleted. Otherwise, once you run the macro there's no undo and deleted rows will be gone.

I get an error that 'i' is not defined. What should it be defined as?
 
Upvote 0
Either delete Option Explicit or add this at the start:

Dim i as Long

When I run the VBA it just says, "No cells were found." It highlights the bolded row below. What I don't understand is that the keywords in the array are indeed gone, but it doesn't select them like I thought it would (as a test run). Not a big deal.

Code:
Option Explicit

Sub Delete_EEE()


Dim Wrds As Variant, Gwrd As String
Dim i As Long


Gwrd = "Jans"
Wrds = Array("ohm", "resistor", "MCKT", "micro", "inductor")


Range("G:G").Replace Gwrd, "#N/A", xlPart, , False


Application.ScreenUpdating = False


For i = LBound(Wrds) To UBound(Wrds)
    Range("E:E").Replace Wrds(i), "#N/A", xlPart, , False
    Range("I:I").Replace Wrds(i), "#N/A", xlPart, , False
Next i


[B]Range("E:I").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Select[/B]


Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
I stepped through the code using F8 and it does appear to be replacing the keywords with #N/A, but then it won't select those rows at the end for me to do a test run.
 
Upvote 0
I stepped through the code using F8 and it does appear to be replacing the keywords with #N/A, but then it won't select those rows at the end for me to do a test run.
What happens when you attempt to execute the line that selects the rows? Do you get an error? If yes, what's the error message? If not, are you certain you haven't clicked on a cell or done something to deselect the rows?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top