Find text and clear contents of another cell

Migaspt

New Member
Joined
Mar 12, 2014
Messages
22
Hello guys,

I'm trying to create a code that has to be run manually.

It should search for the word "total" in column A and clear the contents of the cells in columns K and L in the same row.

I tried searching and using bits of others codes I saw here but I can't seem to find anything useful.

Thanks in advance!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This should work
Code:
Sub ClearColumns()
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("A" & i).Value = "total" Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i
End Sub
or
Code:
Sub ClearColumns2()
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        If InStr(1, Range("A" & i).Value, "total") > 0 Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i
End Sub
or this
Code:
Sub ClearColumnsKL()
    With Range(Cells(1, 1), Cells(Range("A" & Rows.Count).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))
        .AutoFilter Field:=1, Criteria1:="total"
        Range("K:L").SpecialCells(xlCellTypeVisible).ClearContents
        .AutoFilter
    End With
End Sub
 
Last edited:
Upvote 0
Try this:

Sub FindTotalClear()
Range("A1").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1).Select
If ActiveCell = "Total" Then
ActiveCell.Offset(0, 10).ClearContents
ActiveCell.Offset(0, 11).ClearContents
End If
Loop
End Sub
 
Upvote 0
This should work
Code:
Sub ClearColumns()
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("A" & i).Value = "total" Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i
End Sub
or
Code:
Sub ClearColumns2()
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        If InStr(1, Range("A" & i).Value, "total") > 0 Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i
End Sub
or this
Code:
Sub ClearColumnsKL()
    With Range(Cells(1, 1), Cells(Range("A" & Rows.Count).End(xlUp).Row, Cells(1, Columns.Count).End(xlToLeft).Column))
        .AutoFilter Field:=1, Criteria1:="total"
        Range("K:L").SpecialCells(xlCellTypeVisible).ClearContents
        .AutoFilter
    End With
End Sub

Thanks Momentman!

I tried running all of them, the first 2 did not clear any cell.

The last one clears the cells but also the cells of the line with the titles, can we add something for it to clear the contents of the cells starting on line 9 only?
 
Last edited:
Upvote 0
Try this:

Thank you Trevor! This was what I was hoping to get, it finds the cells with "Total" as an exact match, can we add something so it finds the word total and not the entire cell content?

EDIT: Or if it is possible to add the formula =RIGHT(ActiveCell,5) = "total" or something like this it would search for what I need :)
 
Last edited:
Upvote 0
Thanks Momentman!

I tried running all of them, the first 2 did not clear any cell.

The last one clears the cells but also the cells of the line with the titles, can we add something for it to clear the contents of the cells starting on line 9 only?

Well, am not sure why that is, except you didn't explain your request properly

Is total the only word in the cell? is it "Total" or "total"?

The 3 macros work for me as expected anyways

You could use
Code:
Sub ClearColumns2()
    lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    For i = 9 To lastrow
        If InStr(1, Range("A" & i).Value, "Total") > 0 Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i
End Sub
 
Last edited:
Upvote 0
Well, am not sure why that is, except you didn't explain your request properly

Is total the only word in the cell? is it "Total" or "total"?

The 3 macros work for me as expected anyways

I'm sorry. I might have not said everything.

The word I want to search for is "Total", it is not the entire cell content.
 
Upvote 0
I'm sorry. I might have not said everything.

The word I want to search for is "Total", it is not the entire cell content.
You can check the Last macro i posted and see if it works for you
 
Upvote 0
Thanks Momentman, this one does clear the cells that I need:

Code:
[COLOR=#333333]Sub ClearColumns2()[/COLOR]    lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    For i = 9 To lastrow
        If InStr(1, Range("A" & i).Value, "Total") > 0 Then
            Range("K" & i & ":L" & i).Value = ""
        End If
    Next i 
[FONT=Verdana]End Sub[/FONT][COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]
 
Upvote 0
Thank you Trevor! This was what I was hoping to get, it finds the cells with "Total" as an exact match, can we add something so it finds the word total and not the entire cell content?

EDIT: Or if it is possible to add the formula =RIGHT(ActiveCell,5) = "total" or something like this it would search for what I need :)

By the way, I also found that this one works:

Code:
Sub FindTotalClear()Range("A9").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1).Select
If Right(ActiveCell, 5) = "Total" Then
ActiveCell.Offset(0, 10).ClearContents
ActiveCell.Offset(0, 11).ClearContents
End If
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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