I have the below, I have an Excel workbook that has up to 25 worksheets. The code brings in information from a database and depending on the product, anywhere from 1 to 25 of the worksheets are used, that is, the code changes the name of hte worksheet from just, say, "1" to "Lipstick". However, if not all of the worksheets are used, I need to delete those that still have their "number". The sheets are named 1 to 25 before being used (I did not set up this workbook, it is inherited) So. I need to stream line and delete the extra workbooks, but I simply cannot get the below to work. I keep getting a subscript out of range error message.
Public Sub DeleteUnused()
Dim ws As Worksheet, i As Integer
i = 25
For i = 25 To 1 Step -1
If Worksheets(i).Name = Worksheets(" & i & ") Then
Worksheets(" & i & ").Delete