VBA Next without For Error

Excel_GP

New Member
Joined
Sep 1, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I'm getting the Next without For error when trying to run the below macro. For some background, I want to search A1 of every worksheet to see if it contains the text string "- Placeholder" using the wildcard. If the worksheet contains that text string, I want to delete the worksheet. Appreciate any assistance and thanks in advance!

Sub deletesheetbycell()
'Delete Worksheets
Dim shName As String
Dim xName As String
Dim xWs As Worksheet
Dim cnt As Integer
Dim strName As String
Dim blnResult As Boolean

strName = "- Placeholder"

For Each xWs In ThisWorkbook.Sheets
With xWs.Range("A1")
If strName Like "*" & "- Placeholder" & "*" Then
blnResult = True
Else
blnResult = False
End If
If blnResult = True Then
xWs.Delete
cnt = cnt + 1
End If
Next xWs
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please use the VBA button when you copy in the code or we have to manually spend time reformatting the code.
In fact reformatting your code makes it very obvious what is missing.
Here is your code reformatted with the missing End With added.

I haven't analysed the code, so come back to us if it is not doing what you need it to do.

Rich (BB code):
Sub deletesheetbycell()
    'Delete Worksheets
    Dim shName As String
    Dim xName As String
    Dim xWs As Worksheet
    Dim cnt As Integer
    Dim strName As String
    Dim blnResult As Boolean
   
    strName = "- Placeholder"
   
    For Each xWs In ThisWorkbook.Sheets
        With xWs.Range("A1")
            If strName Like "*" & "- Placeholder" & "*" Then
                blnResult = True
            Else
                blnResult = False
            End If
            If blnResult = True Then
                xWs.Delete
                cnt = cnt + 1
            End If
        End With         ' Missing from original code
    Next xWs
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thanks Rich. Much appreciated. The macro is still searching through the entire sheet instead of just A1 on each. Any insight? I assume it's obviously in the For Each section.

VBA Code:
[
Sub deletesheetbycell()
    'Delete Worksheets
    Dim shName As String
    Dim xName As String
    Dim xWs As Worksheet
    Dim cnt As Integer
    Dim strName As String
    Dim blnResult As Boolean
   
    strName = "- Placeholder"
   
    For Each xWs In ThisWorkbook.Sheets
        With xWs.Range("A1")
            If strName Like "*" & "- Placeholder" & "*" Then
                blnResult = True
            Else
                blnResult = False
            End If
            If blnResult = True Then
                xWs.Delete
                cnt = cnt + 1
            End If
[B]      [/B]      End With
    Next xWs
    Application.DisplayAlerts = True
End Sub
]
 
Upvote 0
At the moment you are not looking at A1 or in fact any other cell in the sheet nor are you doing anything with the With xWs.Range("A1") / End With structure.
If you are wanting to check if A1 contains what you have in strName try the below:

Rich (BB code):
    For Each xWs In ThisWorkbook.Sheets
            If xWs.Range("A1") Like "*" & strName & "*" Then
                blnResult = True
            Else
                blnResult = False
            End If
            If blnResult = True Then
                xWs.Delete
                cnt = cnt + 1
            End If
    Next xWs
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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