Delete a particular string defined in Range of Columns across sheets

jack29

New Member
Joined
Apr 28, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi ,

I have managed to write this VBA code, where in to delete the string named "Do Nothing", across all the sheets in the workbook. code without looping each sheet it's working. However, After Inclusion to loop across the worksheets, it's failing to loop through all the sheets, instead only deletes the only sheet which is active. Please kindly help me with the correction of this code to delete a string that has a string "Do Nothing", across all the sheets in a specified Range from K2 to K Last.

VBA Code:
Sub LoopThroughSheetsDeleteString()

'Dim wb As Workbook
Dim ws As Worksheet

For Each ws In Worksheets

    
    Dim rng As Range
    Dim cell As Range
    Dim ContainWord As String

    
    Set rng = Range("K2:K30")

    
    ContainWord = "Do Nothing"

    
    For Each cell In rng.Cells
        If Not cell.Find(ContainWord) Is Nothing Then cell.Clear
    Next cell
Next ws
End Sub
 

Attachments

  • Sample.jpg
    Sample.jpg
    165.6 KB · Views: 19

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try it like
VBA Code:
Sub LoopThroughSheetsDeleteString()

'Dim wb As Workbook
Dim ws As Worksheet

For Each ws In Worksheets

    
    Dim rng As Range
    Dim cell As Range
    Dim ContainWord As String

    
    Set rng = ws.Range("K2:K30")

    
    ContainWord = "Do Nothing"

    
    For Each cell In rng.Cells
        If cell.Value = ContainWord Then cell.Clear
    Next cell
Next ws
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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