Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Problem with If-Then-Else Statement

What is wrong with this code:
If Not Sheets("Harrison 1") Is Nothing Then
Sheets("Harrison 1").Select
Range("A3:C3").Select
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Windows("Day Ahead Obligation.xls").Activate
Range("B6:C29").Select
Application.CutCopyMode = False
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Else
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7:D30").Select
Selection.ClearContents
End If

If I run this on a spreadsheet where there is no "Harrison 1" sheet, I
receive a runtime error 9 "subscript out of range" on the "If Not
Sheets("Harrison 1") Is Nothing Then" statement.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Problem with If-Then-Else Statement

If sheets("harrison 1") doesn't exist, then the reference to Sheets("harrison
1") will cause an error before the "is nothing" is tested.

I like to use something like:

Dim TestWks as worksheet

set testwks = nothing
on error resume next
set testwks = worksheets("harrison 1")
on error goto 0

if testwks is nothing then
'doesn't exist
else
'do the work
end if



DOOGIE wrote:

What is wrong with this code:
If Not Sheets("Harrison 1") Is Nothing Then
Sheets("Harrison 1").Select
Range("A3:C3").Select
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Windows("Day Ahead Obligation.xls").Activate
Range("B6:C29").Select
Application.CutCopyMode = False
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Else
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7:D30").Select
Selection.ClearContents
End If

If I run this on a spreadsheet where there is no "Harrison 1" sheet, I
receive a runtime error 9 "subscript out of range" on the "If Not
Sheets("Harrison 1") Is Nothing Then" statement.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Problem with If-Then-Else Statement

Try this

On Error Resume Next
Set sh = Sheets("Harrison 1")
On Error Goto 0
If Not sh Is Nothing Then

...


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"DOOGIE" wrote in message
...
What is wrong with this code:
If Not Sheets("Harrison 1") Is Nothing Then
Sheets("Harrison 1").Select
Range("A3:C3").Select
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Windows("Day Ahead Obligation.xls").Activate
Range("B6:C29").Select
Application.CutCopyMode = False
Selection.Copy
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Else
Windows("DAY AHEAD LMP SHEET.xls").Activate
Range("C7:D30").Select
Selection.ClearContents
End If

If I run this on a spreadsheet where there is no "Harrison 1" sheet, I
receive a runtime error 9 "subscript out of range" on the "If Not
Sheets("Harrison 1") Is Nothing Then" statement.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with If Statement MarkFrost39 Excel Worksheet Functions 0 September 6th 07 02:52 PM
IF Statement problem MarkFrost39 Excel Worksheet Functions 0 September 6th 07 02:50 PM
IF Statement problem MarkFrost39 Excel Worksheet Functions 0 September 6th 07 02:50 PM
problem with IF statement wolfpack95 Excel Discussion (Misc queries) 1 August 21st 06 08:36 PM
Problem with my Else statement SATATOM Excel Programming 3 November 2nd 04 09:15 PM


All times are GMT +1. The time now is 04:58 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"