Using Worksheet name in VBA

redbug

New Member
Joined
May 28, 2011
Messages
3
I have a little code to check a cell for text, and if the text is NOT there the code deletes the row. Here is the example that works; if the text "keep this" does not appear in the cell (in column D) the entire row is deleted:

Sub Del_Not_Match_Simple()
Dim Rng As Range
Columns(4).Insert
Set Rng = Range([E1], [E65536].End(xlUp)).Offset(, -1)
With Rng
.FormulaR1C1 = "=SEARCH(""keep this"",RC[1])"
On Error Resume Next
.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
On Error GoTo 0
.EntireColumn.Delete
End With
Set Rng = Nothing

End Sub

What I need is for the code to work using the sheet name as the test text. I can't make it work.

Here is an example of the code when I try to use a variable to use the sheet name, the locals window looks OK, but I get errors in all rows.

Sub Del_Not_Match_v1_SheetName()
Dim Rng As Range
Dim SName
SName = ActiveSheet.Name
Columns(4).Insert
Set Rng = Range([E1], [E65536].End(xlUp)).Offset(, -1)
With Rng
.FormulaR1C1 = "=SEARCH(""SName"",RC[1])"
On Error Resume Next
.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
On Error GoTo 0
.EntireColumn.Delete
End With
Set Rng = Nothing
Set SName = Nothing
End Sub

I can't figure out how to get the sheet name to work here. Can anyone help?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to the board.

Try this:

Code:
Sub Del_Not_Match_v1_SheetName()
    Columns("D").Insert
 
    With Intersect(Columns("D"), ActiveSheet.UsedRange)
        .FormulaR1C1 = "=SEARCH(""" & ActiveSheet.Name & """,RC[1])"
        On Error Resume Next
        .SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
        .EntireColumn.Delete
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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