Delete sheet on a condition: debug error

U

ucanalways

I have the following code.

The following sub is written for deleting a worksheet that has its
name as 0. Code works but I dont get the desired result.

Sub test1()

Dim ws as worksheet

For each ws InThisWorkbook.Worksheets

If ws.name = 0 then
Application.DisplayAlerts = False
ws.delete
End If

Next

End Sub

The following sub is written for deleting worksheets if its index
number is greater than 3. Again, Code works but dont get the desired
result.

Sub test2()
Dim ws as worksheet

For each ws InThisWorkbook.Worksheets
Application.DisplayAlerts = False
If ws.index > 3 then
ws.delete
End If

Next

End Sub

Any idea why this happens? Thanks
 
U

ucanalways

Hey there.

What is the desired result?

C

For test1, the desired result would be to delete the sheet that has
the name as 0
For test2, the desired result would be to delete the sheets that has
the index greater thanm 3.

test2 runs quite okay but test1 does not do what I intend to do
 
C

Colby Africa

I get it now. Okay, the worksheet Name property is a string, so try
something like this:



Dim ws As Worksheet


For Each ws In ActiveWorkbook.Worksheets


If ws.Name = "0" Then
Application.DisplayAlerts = False
ws.Delete
End If


Next


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top