I have a VBA application in an Excel Workbook. At the end of the sub I need to delete a chart. In old version of Excel (2013), method "delete" of chart sheet works perfectly. In version 2019 now this method is not supported and I can't find anything on Microsoft website.
Could you help me if exist new method to delete chart sheets?
This is the piece of code at the end of sub
For Each sht In Workbooks(Nam).Sheets
If sht.name <> "Config" Then
Workbooks(Nam).Sheets(sht.name).Delete
End If
Next
When is selected the chart sheet I receive this error:
Run-time errore '1004': Error in method Delete of Chart class
Thanks
CodePudding user response:
It sounds as though you just need to delete the chart sheets from the workbook. I agree that your code looks as though it would, but here is a slightly different approach that may work in your environment.
VBA has three collections for working with sheets in a workbook. Sheets is a collection of all sheets regardless if they are worksheets or chart sheets. Worksheets is the collection of all worksheets. Charts is the collection of all charts. The code below deletes all chart sheets from the current workbook.
Sub delete_chart_sheets()
Dim chrt As Chart
Dim nam As String
nam = ThisWorkbook.Name
Application.DisplayAlerts = False
For Each chrt In Workbooks(nam).Charts
chrt.Delete
Next
End Sub
CodePudding user response:
I have found the issue.
Before the delete of the sheets, I call a function for protect all sheets. Removing this function, now it's works perfectly :D
Thanks to all