2

This problem is driving me crazy. I have the following code:

'unprotect sheet
If.Range("Start").Row+1<.Range("End").Row then
  .Rows(.Range("Start").Row+1 & ":" & .Range("End").Row-1).Select
  Selection.Delete Shift:=xlUp
  'protect sheet
End if

when I run it in debugging mode and trace the code, it works perfectly. But when run the code in a normal mode (not debugging) it gives me an error message as " select method of Range class failed" This errors happens in the line: .Rows(.Range("Start").Row +1 .... that is just after the IF statement. Any ideas? Please help.

Community
  • 1
  • 1
guest1
  • 285
  • 9
  • 17
  • 28
  • 1
    Please use the Code Sample button ("1010 101") to reformat your question. It is very hard to read. – Foole Aug 28 '10 at 23:41

1 Answers1

6

This error usually means you are trying to select a range that belongs to a non-active sheet.

You almost always don't need to select anything:

.Rows(.Range("Start").Row+1 & ":" & .Range("End").Row-1).Delete Shift:=xlUp
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 1
    Perfect! It worked. So, what I did, whenever I select a range anywhere in the code, I activate the corresponding sheet first then I select. Thanks a lot! It was very helptul! – guest1 Aug 29 '10 at 00:15