Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting sheets not working 2

Status
Not open for further replies.

willydude

Programmer
Oct 24, 2006
123
US
This is the kind of problem that drives me crazy. The following works. It deletes all sheets that have the name sheet.
Code:
Sub DeleteWorksheets()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
    If InStr(1, LCase(sheet.Name), "sheet") Then
        sheet.Delete
    End If
Next sheet
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
The following should delete all sheets EXCEPT the ones named "OriginalData" and "SalesPeople".
Code:
Sub CleanCommissionsBySalesBreakDown()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sheet As Worksheet
'On Error Resume Next
For Each sheet In ActiveWorkbook.Worksheets
    If Not InStr(1, LCase(sheet.Name), "OriginalData") Or
Not InStr(1, LCase(sheet.Name), "SalesPeople") Then
    sheet.Delete
    End If
Next sheet
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
The above proc deletes "OriginalData" and
"SalesPeople".

I'm sure that it's simple, but....
 
Replace the Or with And

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Still wants to delete "OriginalData".

I've checked the spelling of the sheet name. It
is accurate. I've switched the order of "OriginalData" and "SalesPeople" in the code.

It's got to be something simple, but not so obvious.
 
You may try this:
If InStr(LCase(sheet.Name), "originaldata") + InStr(LCase(sheet.Name), "salespeople") = 0 Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

This proc works.

Code:
Sub DeleteUnneededWorksheetsSalesPeopleAndDataFromMonthlyCopy()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks("CommsForTesting.xls").Activate
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
    If InStr(1, sheet.Name, "SalesPeople") Or _
        InStr(1, sheet.Name, "OriginalData") Then
        sheet.Delete
    End If
Next sheet
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

This one doesn't.
Code:
Sub CleanCommissionsBySalesBreakDown()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Workbooks("CommissionsBySalesQBTestFile.xls").Activate
Workbooks("CommsForTesting.xls").Activate
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
    If Not InStr(1, sheet.Name, "SalesPeople") Or _
        Not InStr(1, sheet.Name, "OriginalData") Then
        sheet.Delete
    End If
Next sheet
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

With the exception of the NOT keyword, I don't see any difference.
 
Again, replace the Or with And
BTW, have you tried my suggestion stamped 17 Jan 07 17:05 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sorry, hit submit too fast !
The negation of
A Or B
is either
Not A And Not b
or
Not (A Or B)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In such cases it can be good to initially formulate the problem in the positive sense;

If (A=1 Or B=2) then DoStuff

which is relatively easy to get your head around.

If you don't want stuff to happen (in the exactly opposite sense) then add a NOT;

If Not(A=1 Or B=2) then DoStuff

Hugh,
 

I have tried both of the following. Both sets of code WANT to delete "SalesPeople" and/or "OriginalData". They should NOT.
Code:
For Each sheet In ActiveWorkbook.Worksheets
    If Not InStr(1, sheet.Name, "SalesPeople") Or _
        Not InStr(1, sheet.Name, "OriginalData") Then
        'With ActiveSheet
        sheet.Delete
        'End With
    End If
Next sheet
and
Code:
For Each sheet In ActiveWorkbook.Worksheets
    If Not InStr(LCase(sheet.Name), "SalesPeople") Or _
        Not InStr(LCase(sheet.Name), "OriginalData") Then
        With ActiveSheet
        sheet.Delete
        End With
    End If
Next sheet
The following, which is for cleaning up a different wb does work as it should. It DOES delete the two sheets as requested.
Code:
For Each sheet In ActiveWorkbook.Worksheets
    If InStr(1, sheet.Name, "SalesPeople") Or _
        InStr(1, sheet.Name, "OriginalData") Then
        sheet.Delete
    End If
Next sheet
 
OK, I give up. Seems that you make absolutely no sense with the solutions suggested ...
 
PH,

On this one, I have given up also (for the time being).

I have tried the AND and the OR. I have checked my spelling. I do not see how the InStr
will work, but the Not InStr will not.

Thanks for your efforts.

Bill
 
When you use NOT you should be aware that its argument is a boolean value otherwise you may (will) get unexpected (for you) results.

So you are using it with instr as in

Not InStr(LCase(sheet.Name), "SalesPeople")

; supposing InStr returns

0; NOT 0 will return -1 (true)
1; NOT 1 returns -2 (true)
2; NOT 2 returns -3 (true)
etc.
NOT n will only return a zero (False) when its argument is -1 and that is not going to happen when using Instr because it only returns values 0 and positive values.

If you must use NOT with Instr then you should wrap the Instr as follows;

Not (InStr(LCase(sheet.Name), "SalesPeople")=0)

or conversly;

Not (InStr(LCase(sheet.Name), "SalesPeople")>0)

Please read up vba help on the use of logical operators.

HTH Hugh
 
Of course InStr(LCase(sheet.Name), "SalesPeople") will always return zero, suggest you try;

InStr(LCase(sheet.Name), "salespeople")
 
This SKIPS over ALL sheets, including the ones I want to delete. No sheets are deleted.

Code:
For Each sheet In ActiveWorkbook.Worksheets
     If Not (InStr(LCase(sheet.Name), "SalesPeople") = 0) Or _
        Not (InStr(LCase(sheet.Name), "OriginalData") = 0) Then
'        With ActiveSheet
        sheet.Delete
 '       End With
    End If
Next sheet

This wants to delete ALL sheets, including “SalesPeople” and “OriginalData” that I do NOT want to delete.
Code:
For Each sheet In ActiveWorkbook.Worksheets
     If Not InStr(LCase(sheet.Name), "SalesPeople") Or _
        Not InStr(LCase(sheet.Name), "OriginalData") Then
'        With ActiveSheet
        sheet.Delete
 '       End With
    End If

This also want to delete All sheets
Code:
For Each sheet In ActiveWorkbook.Worksheets
     If Not (InStr(LCase(sheet.Name), "SalesPeople") > 0) Or _
        Not (InStr(LCase(sheet.Name), "OriginalData") > 0) Then
'        With ActiveSheet
        sheet.Delete
 '       End With
    End If

As to giving up, I leaning that way myself. I'm heading to lunch for now.

Thanks.

Bill
 
Code:
Sub CleanCommissionsBySalesBreakDown()
   Dim sheet As Worksheet
   
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   
   'Workbooks("CommsForTesting.xls").Activate
   
   For Each sheet In ActiveWorkbook.Worksheets
      If Not (InStr(1, sheet.Name, "SalesPeople") > 0 Or _
              InStr(1, sheet.Name, "OriginalData") > 0) Then
         sheet.Delete
      End If
   Next sheet
   
   Application.DisplayAlerts = True
   Application.ScreenUpdating = True
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top