+ Reply to Thread
Results 1 to 11 of 11

Delete All Worksheets Without String

  1. #1
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Post Delete All Worksheets Without String

    Hello, I'm very new to VBA and I'm trying to amend the below program. This code deletes all worksheets with with "Text". All I need to change is delete all worksheets without "Text". Any help on this would be appreciated.

    Dim i As Integer, n As Integer
    n = ThisWorkbook.Worksheets.Count

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For i = n to 1 step -1
    On Error Resume Next
    If InStr(1, Sheets(i).Name, "Text") Then Sheets(i).Delete
    On Error GoTo 0
    Next i
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

    The worksheets in my model are called Page 1, Page 2, Page 3 ext. The text I am searching for is "CAPITAL PROJECTS". The macro is saved in a different workbook than the macro is being run in called CapitalProjectsAnalysis.xlsx. (Because of a silly company policy I am not allowed to save macro-enabled workbooks) When running the macro, I leave CapitalProjectsAnalysis.xlsx open. Here is what I have tried so far:

    Sub CapitalProjects()

    Dim i As Integer, n As Integer
    n = ThisWorkbook.Worksheets.Count

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For i = n To 1 Step -1
    On Error Resume Next
    If Not InStr(1, Page(i).Name, "CAPITAL PROJECTS") Then Page(i).Delete
    On Error GoTo 0
    Next i
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

    End Sub

    I'm not sure if this code will even work because when I run the macro it returns Compile Error: Sub or Function not defined. I hope this means I'm missing a very simple step.
    Last edited by Mark V.; 05-04-2016 at 02:36 PM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete All Worksheets Without String

    I do not think there is Not on the instr function. You can use less than or greater than instead.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete All Worksheets Without String

    Thank you for the correction.

    Sub CapitalProjects()

    Dim i As Integer, n As Integer
    n = ThisWorkbook.Worksheets.Count

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For i = n To 1 Step -1
    On Error Resume Next
    If InStr(1, Page(i).Name, "CAPITAL PROJECTS", 1) < 0 Then Page(i).Delete
    On Error GoTo 0
    Next i
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

    End Sub

    Now I just need to figure out how to fix the "Compile Error: Sub or Function"

    From the Debugger:

    Capture.PNG

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete All Worksheets Without String

    Yes, "Page" is not defined. What is "Page"? Should not be Sheets(i).?

  5. #5
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete All Worksheets Without String

    Yes, you are correct. I mistakenly though that was where the worksheet name went (Sheet1, Sheet2 ext) and replaced it with my worksheet name (Page 1, Page 2 ext) Anyway, the macro now runs without error, but it isn't deleting any pages. Hopefully I can figure it out from here on my own. Thanks again for your help.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete All Worksheets Without String

    If InStr(1, Sheets(i), "CAPITAL PROJECTS", 1) < 0 Then Sheets(i).Delete
    What the code does is if the sheet name does not have CAPITAL PROJECTS (On the name of the sheet), then the code deletes that sheet.

  7. #7
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete All Worksheets Without String

    Haha, dang it. That is not at all what I need it to do.

    I don't suppose you could point me in the direction of a macro that "searches the contents of each worksheet for "TOTAL CAPITAL PROJECTS:" and deletes every worksheet that does not contain that text string.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete All Worksheets Without String

    Do you mean "TOTAL CAPITAL PROJECTS:" found in any cell, not just the sheet name?

  9. #9
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete All Worksheets Without String

    Correct; if "TOTAL CAPITAL PROJECTS:" is found in any cell, the worksheet is not to be deleted. All other pages are to be deleted. If it helps, the pages are all no bigger than 60 by 80 (A1:BR80)

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete All Worksheets Without String

    Please Login or Register  to view this content.
    If it is not exact match, you need to use this line instead
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-06-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Delete All Worksheets Without String

    Wow, that worked perfectly after I added the inexact line. Thank you very much sir! You just saved me about 23 hours of manually pulling 5-7 pages from 90 50-120 page documents. (7,650 pages total!) Heck, I'll probably use this macro a half dozen more times over the next couples years.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Grab last word from string and then delete word from original string.
    By mtilbury in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-14-2015, 04:14 AM
  2. Replies: 7
    Last Post: 07-25-2014, 08:21 AM
  3. Replies: 1
    Last Post: 11-30-2013, 06:37 AM
  4. Do while loop to delete worksheets if worksheets are not in the specified list
    By kchm_2000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-26-2011, 03:02 PM
  5. Delete rows in all worksheets where column A equals string
    By excelforum123 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-13-2010, 09:25 AM
  6. how to delete a string from another string
    By Ribap in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-23-2006, 06:50 AM
  7. [SOLVED] Delete value in a string
    By Todd Huttenstine in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2006, 03:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1