Search and Delete

G

Guest

Hi all

I need to search through a wookbook for a chart and then if it is there,
delete it. Any ideas as to how to do this? I have tried this but it doesn't
work right.

Set wSheet = Sheets(strSurfPlotName)
If wSheet Is Nothing Then 'Doesn't exist

Set wSheet = Nothing
On Error GoTo 0
Else 'Does exist
Application.DisplayAlerts = False
Charts(strSurfPlotName).Delete
Application.DisplayAlerts = True
Set wSheet = Nothing
On Error GoTo 0
End If

it doesn't search right.

Thanks - Ben H
 
G

Guest

Dim wSheet as Chart
Dim ch as Chart
Dim strSurfPlotName as String
strSurfPlotName = "MyChart"
On Error Resume Next
Set wSheet = Sheets(strSurfPlotName)
On Error goto 0
If wSheet Is Nothing Then 'Doesn't exist
for each ch in Activeworkbook.Charts
if instr(1,ch.name, strSurfPlotName,vbTextCompare) then
ans = msgbox( ch.Name & " appears a close match" _
vbNewLine & "Delete?", vbYesNo)
if ans = vbYes then
Application.DisplayAlerts = False
ch.Delete
Application.DisplayAlerts = True
end if
end if
Next ch
Else 'Does exist
Application.DisplayAlerts = False
Charts(strSurfPlotName).Delete
Application.DisplayAlerts = True
Set wSheet = Nothing
End If
 

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