Refresh the cell styles after deleting unused styles

acpt

New Member
Joined
Dec 14, 2007
Messages
16
Hi

I have created an addin to delete unused styles. This addin is working fine. It is deleting all unused styles as expected. But the problem is that the styles do not go away until I save the workbook and re-open. Is there any way I can avoid this closing and reopening the workbook an instead the deleted styles go away from the toolbar instantly after deleting the styles?

Below is my addin code:


Sub RemoveStyles()
Dim rg As Range, c As Range
Dim sts As Styles, st As Style
Dim collStyles As Collection
Dim wb As Workbook, ws As Worksheet
Dim vSts() As String
Dim i As Long
'Set wb = ThisWorkbook
Set wb = Workbooks(1)
Set collStyles = New Collection

'Get list of used styles
On Error Resume Next

Application.ScreenUpdating = True

For Each ws In wb.Worksheets
Set rg = ws.UsedRange
For Each c In rg
collStyles.Add Item:=c.Style, Key:=CStr(c.Style)
Next c
Next ws
On Error GoTo 0

'Delete unused styles
'Set sts = ThisWorkbook.Styles
Set sts = Workbooks(1).Styles
For Each st In sts
On Error GoTo DeleteStyle
Debug.Print collStyles.Item(st.Name)
On Error GoTo 0

Next st

'Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
ActiveWorkbook.Activate

Exit Sub
DeleteStyle:
st.Delete
Resume Next
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,216,186
Messages
6,129,393
Members
449,507
Latest member
rjwalker1973

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top