Results 1 to 11 of 11

Delete All Worksheets Without String

Threaded View

  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.

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