Option Explicit
Sub xlDelCharts( _
Optional xlBookName As String, _
Optional xlSheetName As String, _
Optional InformUser As Boolean = True)
Dim I As Integer
Dim MsgBxTitle As String
Dim NumCharts As Integer
Dim Rtn As VbMsgBoxResult
Dim xlBook As Workbook
Dim xlSheet As Worksheet
MsgBxTitle = "Delete Charts in Worksheet"
On Error GoTo xlBookError
If xlBookName = vbNullString Then xlBookName = ActiveWorkbook.Name
Set xlBook = Workbooks(xlBookName)
On Error GoTo xlSheetError
If xlSheetName = vbNullString Then xlSheetName = ActiveSheet.Name
Set xlSheet = xlBook.Worksheets(xlSheetName)
On Error GoTo ErrorHandling
NumCharts = xlSheet.ChartObjects.Count
If NumCharts < 1 Then
If InformUser = True Then
MsgBox "There are no embedded chart to delete in worksheet " & _
xlSheetName, vbInformation, MsgBxTitle
End If
Exit Sub
End If
If InformUser = True Then
Rtn = MsgBox("This procedure will delete all charts" & vbCrLf & _
"embedded in worksheet " & ActiveSheet.Name & vbCrLf & vbCrLf & _
"# charts to be deleted " & NumCharts & vbCrLf & vbCrLf & _
"OK ?", vbQuestion & vbYesNo, MsgBxTitle)
If Rtn <> vbYes Then Exit Sub
End If
For I = NumCharts To 1 Step -1
xlSheet.ChartObjects(I).Delete
Next I
If InformUser = True Then
MsgBox "xlDelCharts" & vbCrLf & vbCrLf & _
"workbook: " & xlBookName & vbCrLf & _
"worksheet: " & xlSheetName & vbCrLf & _
NumCharts & " chart object(s) successfully deleted", _
vbInformation, MsgBxTitle
End If
Exit Sub
xlBookError:
MsgBox "xlDelCharts: workbook specified is " & xlBookName & vbCrLf & _
"That workbook is not presently open" & vbCrLf & vbCrLf & _
"No actions taken", vbCritical, MsgBxTitle
Exit Sub
xlSheetError:
MsgBox "xlDelCharts: worksheet specified is " & xlSheetName & vbCrLf & _
"That sheet is either not part of " & xlBookName & " or is" & vbCrLf & _
"a chartsheet." & vbCrLf & vbCrLf & _
"No actions taken", vbCritical, MsgBxTitle
Exit Sub
ErrorHandling:
MsgBox "xlDelCharts: error encountered; err = " & Str(Err), vbCritical, _
MsgBxTitle
End Sub
Sub xlDelChartsBook( _
Optional xlBookName As String, _
Optional InformUserSheet As Boolean = False, _
Optional InformUserBook As Boolean = True, _
Optional DelWhich As String = "both")
Dim MsgBxTitle As String
Dim NumCharts As Integer
Dim NumChSheets As Integer
Dim NumSheets As Integer
Dim Rtn As VbMsgBoxResult
Dim strBuffer As String
Dim xlBook As Workbook
Dim xlChSheet As Chart
Dim xlSheet As Worksheet
MsgBxTitle = "Delete Charts in Workbook"
On Error GoTo xlBookError
If xlBookName = vbNullString Then xlBookName = ActiveWorkbook.Name
Set xlBook = Workbooks(xlBookName)
Select Case DelWhich
Case "charts", "chartsheets", "both"
Case Else
MsgBox "xlDelChartsBook: DelWhich arguement is invalid" & vbCrLf & vbCrLf & _
"No actions taken", vbCritical, MsgBxTitle
Exit Sub
End Select
On Error GoTo ErrorHandling
If InformUserBook = True Then
NumSheets = xlBook.Worksheets.Count
For Each xlSheet In xlBook.Worksheets
strBuffer = strBuffer & " " & xlSheet.Name & " " & _
xlSheet.ChartObjects.Count & vbCrLf
NumCharts = NumCharts + xlSheet.ChartObjects.Count
Next xlSheet
strBuffer = strBuffer & vbCrLf & "ChartSheets to be deleted:" & vbCrLf
NumChSheets = xlBook.Charts.Count
If NumChSheets > 0 Then
For Each xlChSheet In xlBook.Charts
strBuffer = strBuffer & " " & xlChSheet.Name & vbCrLf
Next xlChSheet
strBuffer = strBuffer & vbCrLf & _
"NOTE: Excel would normally warn you about the impending" & vbCrLf & _
"deletion of any chartsheet. Those warnings will be turned" & vbCrLf & _
"off to eliminate the need for you to respond to each prompt" & vbCrLf & _
"and turned back on after deleting the charsheets." & vbCrLf
End If
Rtn = MsgBox("This procedure will delete all charts and chartsheets" & vbCrLf & _
"in workbook " & xlBookName & vbCrLf & vbCrLf & _
"Worksheets to be examined and charts in each: " & vbCrLf & strBuffer & _
vbCrLf & "OK ?" & vbCrLf & vbCrLf & _
"[select NO if you want to delete just charts or just charsheets]" & vbCrLf & _
"[select CANCEL if you want to stop the process and exit]", _
vbQuestion & vbYesNoCancel, MsgBxTitle)
Select Case Rtn
Case vbCancel
Exit Sub
Case vbNo
DelWhich = _
InputBox("enter 'charts' to delete just charts" & vbCrLf & _
"enter 'chartsheets' to delete just chartsheets" & vbCrLf & _
"enter 'both' to delete both" & vbCrLf & vbCrLf & _
"any other entry or blank or Cancel button will" & vbCrLf & _
"stop the procedure without deleting anything", MsgBxTitle)
DelWhich = LCase(DelWhich)
Select Case DelWhich
Case "charts", "chartsheets", "both"
Case Else
Exit Sub
End Select
Case vbYes
Case Else
Exit Sub
End Select
End If
If DelWhich = "charts" Or DelWhich = "both" Then
For Each xlSheet In xlBook.Worksheets
Call xlDelCharts(xlBookName, xlSheet.Name, InformUserSheet)
Next xlSheet
End If
If DelWhich = "chartsheets" Or DelWhich = "both" Then
Application.DisplayAlerts = False
For Each xlChSheet In xlBook.Charts
xlChSheet.Delete
Next xlChSheet
Application.DisplayAlerts = True
End If
If InformUserBook = True Then
If DelWhich = "charts" Then NumChSheets = 0
If DelWhich = "chartsheets" Then
NumSheets = 0
NumCharts = 0
End If
MsgBox "xlDelChartsBook" & vbCrLf & vbCrLf & _
"workbook: " & xlBookName & vbCrLf & _
"# worksheets processed: " & NumSheets & vbCrLf & _
"# charts deleted: " & NumCharts & vbCrLf & _
"# chartsheets deleted: " & NumChSheets, _
vbInformation, MsgBxTitle
End If
Exit Sub
xlBookError:
MsgBox "xlDelChartsBook: workbook specified is " & xlBookName & vbCrLf & _
"That workbook is not presently open" & vbCrLf & vbCrLf & _
"No actions taken", vbCritical, MsgBxTitle
Exit Sub
ErrorHandling:
MsgBox "xlDelChartsBook: error encountered; err = " & Str(Err), vbCritical, _
MsgBxTitle
End Sub
|