vba code to search and clear contents

londa_vba

Board Regular
Joined
May 11, 2023
Messages
61
Office Version
  1. 365
Platform
  1. Windows
vba code to search column B for the words "Thermal Mixer" and when found to clear the contents (not delete) of that row. The row where "Thermal Mixer" is found will vary and there may be multiple instances on the sheet. Main point is to keep the row count the same.

from this

23-00017068.1Angela Sanders~1/2 swab (2)100
23-00028112.1Carl Cook~1/2 swab (2)100
23-0003RB1Reagent Blank 1100
Thermal MixerTemp In:00:00
Thermal MixerTemp In:00:00
23-00049018.1Miles Davis~1/2 swab (2)100
23-00056022.1Sarah Anderson~1/2 swab (2)100
23-0006RB1Reagent Blank 1100
Thermal MixerTemp In:00:00
Thermal MixerTemp In:00:00

to this

23-00017068.1Angela Sanders~1/2 swab (2)100
23-00028112.1Carl Cook~1/2 swab (2)100
23-0003RB1Reagent Blank 1100
23-00049018.1Miles Davis~1/2 swab (2)100
23-00056022.1Sarah Anderson~1/2 swab (2)100
23-0006RB1Reagent Blank 1100

thank you for any suggestions
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello,

Try below code
VBA Code:
Sub ForEachCell()
    Dim Cell As Range
    
    For Each Cell In Sheets("Sheet1").Range("A1:F12")
        If Cell.Value = "Thermal Mixer" Then Cell.EntireRow.ClearContents
    Next Cell
    
End Sub
 
Upvote 0
Hello,

Try below code
VBA Code:
Sub ForEachCell()
    Dim Cell As Range
   
    For Each Cell In Sheets("Sheet1").Range("A1:F12")
        If Cell.Value = "Thermal Mixer" Then Cell.EntireRow.ClearContents
    Next Cell
   
End Sub
Thank you for the response but this is not working for me. I even changed "Sheet1" to my actual sheet name and nothing
 
Upvote 0
Hi @londa_vba . I hope you are well.

Try this.
I assume you have header, so change the 1 in this line if the header is in another row.
ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.ClearContents

VBA Code:
Sub Macro2()
  ActiveSheet.Range("A1:B" & Range("B" & Rows.Count).End(3).Row).AutoFilter 2, "=*Thermal Mixer*"
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.ClearContents
  Range("A1").AutoFilter
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
Hi @londa_vba . I hope you are well.

Try this.
I assume you have header, so change the 1 in this line if the header is in another row.
ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.ClearContents

VBA Code:
Sub Macro2()
  ActiveSheet.Range("A1:B" & Range("B" & Rows.Count).End(3).Row).AutoFilter 2, "=*Thermal Mixer*"
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.ClearContents
  Range("A1").AutoFilter
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Yes that was the issue. I had a header in row 1 and 2 so setting offset to 2 worked for me. Thank you
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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