Link to home
Start Free TrialLog in
Avatar of shelldee
shelldeeFlag for Canada

asked on

Excel 2010: Delete empty "Sheets" from VBA Project

Hi Everyone,

I've been doing some cleanup, and now have empty "Sheets" in Project Explorer that aren't associated to an Excel worksheet (they've been deleted). Also, there is no code on the "Sheets", either. I did have the code protected when I was deleting sheets.... could this be why they weren't removed from the VBA Project when the worksheets were deleted?

 I have run Rob Bovey's code cleaner, but it didn't resolve this. Does anyone know how to remove these ... just out of curiosity... the functionality hasn't been affected.
Here's a screenshot:
 User generated image
Thanks so much,
Shelley
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

On a standard module place the following code and run it to make sure that those sheets are not hidden.

Sub ShowSheets()
Dim ws As Worksheet
For Each ws In Worksheets
    ws.Visible = True
Next ws
End Sub

Open in new window

have you tried closing Excel and re-opening?

Pritecting the code will not stop sheets being deleted.
Avatar of shelldee

ASKER

Hi sktneer,

Thanks for responding :)

I ran your code -- and checked to see if any sheets showed up -- but they didn't. Was this to check if the visible property was set to xlSheetVeryHidden?

The thing that seems weird to me is that all of these sheets'/objects' icons have changed to the one that represents ThisWorkbook -- plus the codenames of all of the objects are also now "ThisWorkbook".

Thanks for your help!
Shelley
Hi Roy,

Yes I've tried closing and reopening a couple of times.

Thanks for your help,
Shelley
xlSheetVeryHidden will not matter, the code will make all the hidden sheets visible.

Create a backup file and then try the following code to see what happens in this case.

Sub DeleteEmptySheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
    If ws.Visible = xlSheetVeryHidden Then ws.Delete
Next ws
Application.DisplayAlerts = True
End Sub

Open in new window

You should try this as the above code may produce an error if the sheet is xlSheetVeryHidden....

Sub DeleteEmptySheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
    If ws.Visible = xlSheetVeryHidden Then
        ws.Visible = xlSheetVisible
        ws.Delete
    End If
Next ws
Application.DisplayAlerts = True
End Sub

Open in new window

Hi skteer,

I ran your code twice, the second time butting put a Debug.Print ws.CodeName before the IF statement to see if these "Sheets" were being checked in the loop. None of the "Sheets" in question were printed to the immediate window.

Thanks again,
Shelley
ASKER CERTIFIED SOLUTION
Avatar of shelldee
shelldee
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah not a bad idea in case if nothing is working in favor.

But that's very strange because as far as I am aware if you see the sheets in the project explorer that means the sheets exist in the workbook.

Can you share the workbook after removing all the sensitive information from it, I am just curious to see the file.
If any issue uploading it here, you can PM me and attach your sample workbook.
Thanks for the offer sktneer! :)

Since i can't send any of the data, I'll move everything, and then send you what's left. I probably won't be able to have it ready until tonight sometime, tho -- since I need to do other work this afternoon.

Thanks again!
Shelley
No problem Shelley!

I think you should continue with what you have planned.


Regards.
Thanks -- sounds good -- and I'll repost when the file is ready to send.
Cheers :)
Shelley
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.