Loop - omissions

  • Thread starter Thread starter Yarroll
  • Start date Start date
Y

Yarroll

Hi,

What on earth is wrong with the followig lines: (this is part of a macro)

wbkBook.Activate
For Each x In ActiveWorkbook.Sheets
If Not x.Name = "Summary" Or x.Name = "Summary1" Then
x.Activate
Rows("2:2500").Delete Shift:=xlUp
End If
Next x

IOW, I need to exclude Sheets "Summary", "Summary1" from row deletion.
Excel goes ahead and deletes rows in both of these.

Thanks, Yarroll
 
Look up "Operator Precedence" in XL/VBA Help.

"Not" has a higher precedence than "Or", so


Not x.Name = "Summary" Or x.Name = "Summary1"

is equivalent to

(Not x.Name = "Summary") Or x.Name = "Summary1"

rather than

Not (x.Name = "Summary" Or x.Name = "Summary1")

So when x.Name = Summary, the evaluation is:

(Not True) Or False ===> False

and when x.Name = Summary1

(Not False) Or True ===> True
 
"JE McGimpsey"
Look up "Operator Precedence" in XL/VBA Help.

"Not" has a higher precedence than "Or", so

(snip)
Thank you very much!! I was already losing it :-((
Best regards, Yarroll
 
Just personal preference would be to use a Select Case statement.

Sub Demo()
Dim sht
For Each sht In ActiveWorkbook.Sheets
Select Case sht.Name
Case "Summary", "Summary1"
'Do nothing
Case Else
sht.Rows("2:2500").Delete
End Select
Next sht
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top