Test to see if sheet exists fails to test a graph sheet.

raven_squire

Board Regular
Joined
Jan 13, 2013
Messages
99
Hello In the following code I am trying to see if a worksheet exists and if not I want to create a graph as a worksheet (I am not sure what the propper term for this is). However the test does not test the worksheet that contains the graph only the regular worksheets. Any help to determin if the graph workshees already exists would be apreciated.

Thanks Steve

Code:
'See if the chart sheet exists
Debug.Print "******"
For Each Sheet In Worksheets
    Debug.Print Sheet.Name
    If Sheet.Name = line & "_" & Direction & "_" & Class Then
        MsgBox "The graph that you are trying to generate already exists." & Chr(13) & "Please delete the graph and try again.) "
        Debug.Print "True"
        End
    Else
    Debug.Print "fail"
    End If
Next Sheet

Charts.Add Before:=Worksheets(Worksheets.Count)

With ActiveChart
    
    'Chart Specifics
    .ChartType = xlXYScatter
    .Location Where:=xlLocationAsNewSheet, Name:=line & "_" & Direction & "_" & Class
    .HasTitle = True
    .ChartTitle.Characters.Text = ChartXtitle
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,
your For Next loop is only testing for worksheets.

If you want to include Chart Sheets in the test then try changing code as follows:


Rich (BB code):
Dim Sheet As Object
For Each Sheet In Sheets
    ' rest of code
Next Sheet

Hopefully, this will do what you want.

Dave
 
Upvote 0
【For Each Sheet In Worksheets】 maybe change Worksheets to sheets will solve your problem.
Sheets include normal worksheet and graph sheet, Worksheets only including normal worksheets.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top