+ Reply to Thread
Results 1 to 10 of 10

Do - Loop (Sheet Delete Issue)

Hybrid View

HerryMarkowitz Do - Loop (Sheet Delete Issue) 09-08-2014, 01:01 PM
xladept Re: Do - Loop (Sheet Delete... 09-08-2014, 01:10 PM
shg Re: Do - Loop (Sheet Delete... 09-08-2014, 01:11 PM
stnkynts Re: Do - Loop (Sheet Delete... 09-08-2014, 01:14 PM
mehmetcik Re: Do - Loop (Sheet Delete... 09-08-2014, 01:18 PM
HerryMarkowitz Re: Do - Loop (Sheet Delete... 09-08-2014, 01:19 PM
HerryMarkowitz Re: Do - Loop (Sheet Delete... 09-08-2014, 01:57 PM
stnkynts Re: Do - Loop (Sheet Delete... 09-08-2014, 02:05 PM
HerryMarkowitz Re: Do - Loop (Sheet Delete... 09-08-2014, 02:37 PM
stnkynts Re: Do - Loop (Sheet Delete... 09-08-2014, 03:45 PM
  1. #1
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Do - Loop (Sheet Delete Issue)

    Greetings,
    1- Open enclosed file.
    2- Run Macro1.
    Sub Macro1()
    Dim ws As Worksheet
    Set ws = Worksheets(1)
    Do Until ws.Name = "Main"
        ws.Select
        Application.DisplayAlerts = False
        If Range("A1").Value Like "apple" Then ws.Delete
        Application.DisplayAlerts = True
        Set ws = ws.Next
    Loop
    End Sub
    After running Macro1 you will see that there is an error on red line...

    Any support will be appreciated.
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Do - Loop (Sheet Delete Issue)

    See if this works for you:

    Sub Macro1()
    Dim ws As Worksheet
    For Each ws in Worksheets
    If ws.Name = "Main" Then
        If Range("A1").Value Like "apple" Then
     ws.Delete:Exit Sub:End If:Next
        End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Do - Loop (Sheet Delete Issue)

    When you delete the worksheet, the object variable is disconnected from the object, so there is no ws.Next.

    As generally for deleting things, loop backwards:

    Sub Macro1()
        Dim i           As Long
        Dim wks         As Worksheet
    
        Application.DisplayAlerts = False
        For i = Worksheets("Main").Index - 1 To 1 Step -1
            Set wks = Worksheets(i)
            If LCase(wks.Range("A1").Value) = "apple" Then wks.Delete
        Next i
    
        Application.DisplayAlerts = True
    End Sub
    Last edited by shg; 09-08-2014 at 01:13 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Do - Loop (Sheet Delete Issue)

    Sub Macro1()
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        If ws.Name = "Main" Then Exit Sub
        Application.DisplayAlerts = False
            If Not InStr(1, ws.Range("A1").Value, "apple") = 0 Then ws.Delete
        Application.DisplayAlerts = True
    Next
    
    End Sub

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Do - Loop (Sheet Delete Issue)

    Sorry Harry

    You have defined ws as worksheet 1

    You then deleted worksheet 1

    Then you ask excel to switch to the next worksheet 1.

    Yes I can see why you would get an error.


    
    Sub macro1()
    
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    ws.Select
    Next ws
    End Sub

  6. #6
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Do - Loop (Sheet Delete Issue)

    Very thanks...Solved...

  7. #7
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Do - Loop (Sheet Delete Issue)

    Hi shg,
    There will be any diffrence if I use
     If wks.Range("A1").Value Like "apple" Then wks.Delete
    instead of
     If LCase(wks.Range("A1").Value) = "apple" Then wks.Delete
    ???

  8. #8
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Do - Loop (Sheet Delete Issue)

    There will be any diffrence if I use
    Yes.

    The first code will only have a positive match with: "apple"
    The second code with have a positive match with: "APPLE", "apple", "Apple", "APPle", etc

  9. #9
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: Do - Loop (Sheet Delete Issue)

    Ok..
    What is InStr for ?

     If Not InStr(1, ws.Range("A1").Value, "apple") = 0 Then ws.Delete

  10. #10
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Do - Loop (Sheet Delete Issue)

    using Instr will return a positive match with: "apple", "aaappleee", "this is an apple", "sappled", etc

+ 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. Loop to Delete worksheets stops after deleting one sheet...
    By booDaddy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2014, 05:12 PM
  2. [SOLVED] Delete Sheet before Importing Sheet calculation issue
    By aetedford in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-27-2013, 02:49 PM
  3. [SOLVED] Loop Through Sheet and Delete Rows
    By mlb723 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2012, 06:00 PM
  4. [SOLVED] Loop through entire sheet, Insert row, copy row data, paste row data, delete row
    By Hyperdude in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2012, 01:46 AM
  5. [SOLVED] Issue with For Each loop
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-16-2012, 09:01 PM

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