E=mc2
asked on
How can I delete rows with specific data found in column A using a Macro in Excel?
What commands in a macro can I use to delete specific data it finds in Column A?
For instance if the following words are found, how can I delete the entire row?
'Run
For instance if the following words are found, how can I delete the entire row?
'Run
ASKER
This does not work.
Compile Error:
Sub or Function not defined.
Compile Error:
Sub or Function not defined.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can change it with this
cCell.EntireRow.Delete
And if there are many instances of Run in Column A this code will take care of deleting all rows that contains Run in Column A
gowflow
Sub DeleteSpecific()
Dim cCell As Range
Dim FirstAddress As String
With ActiveSheet.Range("A:A")
Set cCell = .Find(What:="Run", LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)
If Not cCell Is Nothing Then
FirstAddress = cCell.Address
Do
ActiveSheet.Range("A" & cCell.Row).EntireRow.Delete
Set cCell = .FindNext(cCell)
Loop While Not cCell Is Nothing And cCell.Address <> FirstAddress
End If
End With
End Sub
gowflow
@gowflow , You can simplify your code as this,
Sub DeleteSpecific()
Dim cCell As Range
With ActiveSheet.Range("A:A")
Set cCell = .Find(What:="Run", lookin:=xlValues, Lookat:=xlWhole, MatchCase:=False)
Do While Not cCell Is Nothing
cCell.EntireRow.Delete
Set cCell = .FindNext()
Loop
End With
End Sub
And i made it a little more general & regular by giving user access to define range, text for looking up and some optional parameters of find function.
Sub DeleteSpecific()
RegularDeleteSpecific ActiveSheet.Range("A:A"), "Run"
End Sub
Sub RegularDeleteSpecific(ByRef xLookInRange As Range, ByVal xLookFor As String, Optional ByVal xLookIn As XlFindLookIn = xlValues, Optional ByVal xLookAt As XlLookAt = xlWhole, Optional ByVal xMatchCase As Boolean = False)
Dim cCell As Range
Set cCell = xLookInRange.Find(What:=xLookFor, lookin:=xLookIn, LookAt:=xLookAt, MatchCase:=xMatchCase)
Do While Not cCell Is Nothing
cCell.EntireRow.Delete
Set cCell = xLookInRange.FindNext()
Loop
End Sub
@Hakan Yilmaz
With all due respect and not to offend in anyway, we are in a forum where if you feel you have a solution that could contribute to answers given and not already given then it is welcomed. Answers that simply changes or modify an answer given does not contribute in providing variety nor add more to what is proposed. Moreover there sometimes could be several possibilities in writing a solution hence changing a proposed solution is just simply not productive.
gowflow
With all due respect and not to offend in anyway, we are in a forum where if you feel you have a solution that could contribute to answers given and not already given then it is welcomed. Answers that simply changes or modify an answer given does not contribute in providing variety nor add more to what is proposed. Moreover there sometimes could be several possibilities in writing a solution hence changing a proposed solution is just simply not productive.
gowflow
Everyone can see whats going on with answers given for this question, and i know that.
You gave a nice solution first. I added some more functionality later.
So this thread has more information for future visitors.
It's clear that i modified your code. You call it "simply modifying". But I still call it a "contribution". Because someone can learn different things from my posts.
(Not about this topic, but) What i generally want to do is to make more people learn something from EE, so it is not between "authors" and "solvers" of a thread.
Also, I want more general solutions. In other words, I want to generalize solutions.
So that they will no longer be specific answers for a case.
I'm sorry that i don't know English sufficiently enough to speak more politely, but I'm trying to be polite.
You gave a nice solution first. I added some more functionality later.
So this thread has more information for future visitors.
It's clear that i modified your code. You call it "simply modifying". But I still call it a "contribution". Because someone can learn different things from my posts.
(Not about this topic, but) What i generally want to do is to make more people learn something from EE, so it is not between "authors" and "solvers" of a thread.
Also, I want more general solutions. In other words, I want to generalize solutions.
So that they will no longer be specific answers for a case.
I'm sorry that i don't know English sufficiently enough to speak more politely, but I'm trying to be polite.
Open in new window
BTW is it Run or 'Run ??? if latter then replace this line
set Cell = Activesheet.Range("A:A").F
by this line
set Cell = Activesheet.Range("A:A").F
gowflow