How to delete a style?

J

JE McGimpsey

One way:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Me.Styles("myStyleName").Delete
On Error GoTo 0
End Sub

Use the On Error Resume Next in case the user deletes the style before
closing.

You could also prevent the error on startup in a similar way:

Private Sub Workbook_Open()
On Error Resume Next
With Me.Styles.Add("myStyleName")
.Font.Bold = True
.Font.Size = 16
End With
On Error GoTo 0
End Sub
 
J

JE McGimpsey

One way you could have found this would have been to record a macro of
manually deleting the style. The macro recorder gives the correct method.
 
S

serdar

(Briefly, how to delete a style when i close the workbook?)

I create a style as:

'-----------
Public myStyle As Style

Private Sub Workbook_Open()

Set myStyle = ThisWorkbook.Styles.Add("myStyleName")

With myStyle
.Font.Bold = True
.Font.Size = 16
End With

End Sub
'------------

When i close and reopen the workbook i get an error cos this code attemps to
create the same style again. The styles created remains even i quit excel. I
checked it with:

For i = 1 To ActiveWorkbook.Styles.Count
Worksheets(1).Cells(i , 1) = ActiveWorkbook.Styles(i).Name
Next

How to delete a style when i close the workbook?
 
S

serdar

Thanks.I was trying it by disposing the object as:

myStyle.delete

and did not worked.
 
Top