PDA

View Full Version : Solved: delete sheet based on cell value



thole
04-07-2008, 05:50 AM
I want to delete a sheet if its name equals a certain cell's value.
Ihave the following code; which gives me an error when trying to delete the sheet:



Sub DeleteWorksheet()

Worksheets("Hidden_Calculations").Select
Last = Cells(Rows.Count, "B").End(xlUp).Row
For i = Last To 10 Step -1
If (Cells(i, "B").Value) = [Sheet2!$c$6] Then
Cells(i, "B").EntireRow.Delete
End If
Next i

For Each sht In ActiveWorkbook.Sheets

If Sheet.Name = Sheets(Sheet2).Range("$C$6").Value Then sht.Delete


Next
End Sub

Simon Lloyd
04-07-2008, 06:13 AM
Perhaps this line:
If Sheet.Name = Sheets(Sheet2).Range("$C$6").Value Then sht.Delete
should look like this:

If sht.Name = Sheets("Sheet2").Range("$C$6").Value Then sht.Delete

thole
04-07-2008, 06:19 AM
Unfortunately, it doesn't work; I get a type error mismatch, and only the part
below is highlighted.


If sht.Name = Sheets(Sheet2).Range("$C$6").Value Then

MikeO
04-07-2008, 06:44 AM
Unfortunately, it doesn't work; I get a type error mismatch, and only the part
below is highlighted.


If sht.Name = Sheets(Sheet2).Range("$C$6").Value Then


Look closer at Simon's suggestion. You need to put "Sheet2" in quotation marks.

thole
04-07-2008, 07:00 AM
No, still doesn't work.
Thanks about catching that, though (it's morning here - too little sleep and not enough coffee...)

Simon Lloyd
04-07-2008, 08:05 AM
When you say it doesnt work have you tried stepping through the code and finding out which part of the code is faulting? Also you haven't Declared your variable add this line to the top of your code under the sub

Dim sht As Worksheet

thole
04-07-2008, 08:23 AM
worked like a charm. thank you!