Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello, I have a macro that deletes a row if it contains certain text in a
cell. I need to convert it to delete every row that does not have the certain text in a cell. So for the selected range, delete every row that does not have a cell that says "cost". Thanks, Todd. Sub DeleteRowifText() Dim rng As Range Dim what As String what = "cost" Do Set rng = ActiveSheet.UsedRange.Find(what) If rng Is Nothing Then Exit Do Else Rows(rng.Row).Delete End If Loop End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Todd:
try Sub DeleteRowifText() Dim rng As Range Dim what As String what = "cost" For i = LastRow To 1 Step -1 If Application.WorksheetFunction.CountIf(Rows(i), what) = 1 Then Rows(i).Delete 'or Rows(i).Delete Shift:=xlUp End If Next i End Sub Function LastRow() As Integer LastRow = Cells.Find(what:="*", After:=[A1], SearchDirection:=xlPrevious).Rows.Row End Function -- 天行健,君*以自強不息 地勢坤,君*以厚德載物 http://www.vba.com.tw/plog/ "Todd" wrote: Hello, I have a macro that deletes a row if it contains certain text in a cell. I need to convert it to delete every row that does not have the certain text in a cell. So for the selected range, delete every row that does not have a cell that says "cost". Thanks, Todd. Sub DeleteRowifText() Dim rng As Range Dim what As String what = "cost" Do Set rng = ActiveSheet.UsedRange.Find(what) If rng Is Nothing Then Exit Do Else Rows(rng.Row).Delete End If Loop End Sub |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
One way:
Option Explicit Sub DeleteRowifNoText() Dim rng As Range Dim whatToFind As String Dim FirstRow As Long Dim LastRow As Long Dim iRow As Long whatToFind = "cost" With ActiveSheet FirstRow = 1 'no headers? LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For iRow = LastRow To FirstRow Step -1 With .Rows(iRow) Set rng = .Find(what:=whatToFind, after:=.Cells(1), _ LookIn:=xlFormulas, lookat:=xlWhole, _ searchorder:=xlByColumns, _ searchdirection:=xlNext, MatchCase:=False) End With If rng Is Nothing Then .Rows(iRow).Delete End If Next iRow End With End Sub I used column A to find the last used row. And I specified the parms to the .find statement. If you don't specify them, excel will use the same parms as the previous Find (either from code or by the user). Todd wrote: Hello, I have a macro that deletes a row if it contains certain text in a cell. I need to convert it to delete every row that does not have the certain text in a cell. So for the selected range, delete every row that does not have a cell that says "cost". Thanks, Todd. Sub DeleteRowifText() Dim rng As Range Dim what As String what = "cost" Do Set rng = ActiveSheet.UsedRange.Find(what) If rng Is Nothing Then Exit Do Else Rows(rng.Row).Delete End If Loop End Sub -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell | Excel Discussion (Misc queries) | |||
Excel "Move or Copy" and "Delete" sheet functions | Excel Worksheet Functions | |||
Macro to Replace/Delete Text Using "Watchword" List? | Excel Discussion (Misc queries) | |||
Macro to concatenate into "B1" B2 thru B"x" based on new data in "Col A" | Excel Discussion (Misc queries) | |||
Adding "New" "Insert" "Delete" into a workbook to change from data 1 to data 2 etc | Excel Programming |