Hello,
I am trying to write a code which checks if a chart (named "Friction - Chart "plotted as a seperate sheet) is existing. If yes, the code deletes the chart sheet. However my code fails to that. If however I try to do the same with another sheet within the workbook called "Friction" (it contains just 2 columns of data), it executes it perfectly. So I am at a loss trying to figure out why the code works for one sheet but not the other. Below is the code:
Code:
Sub deletesheet()
Dim sh As Worksheet
' Delete any existing charts.
For Each sh In ThisWorkbook.Worksheets
If sh.Name Like "Friction - Chart" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next sh
End Sub
I am also attaching the excel spreadsheet with the macro. Can somebody please help me figure out why such a thing is happening and suggests changes to the code?
Thanks
Hi parasbshah,
It looks like your chart is a "Chart" not a "Worksheet"
Looking through the object browser (I hate the thing but it helps) it shows you need to use
For each Chart instead of Worksheet.
In the immediate window do a
?charts(1).Name
to see it is a chart and not a worksheet.
hope that helps.
One test is worth a thousand opinions.
Click the * below to say thanks.
Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
hi
try this
Sub Button1_Click() ActiveWorkbook.Charts.Delete End Sub
Regards, John
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks