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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is ActiveSheet a Chart 4

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I have several sheets some of which are filled with Charts (i.e. chart on new worksheet idea), some are not. I need to check if the ActiveSheet is a chart. Is there a way to do this? Clive [infinity]
 
if activeworksheet.type=-4169 then
msgbox "It's a chart!"
end if

Strangely, VBA help doesn't define an Excel constant for the -4169, but it works.
Rob
[flowerface]
 
Clive,

Wasn't sure if you are trying to distinguish between Chart sheets and worksheets containing embedded charts. The following will identify both:

Code:
Const SheetType_Worksheet = -4167
Const SheetType_Chart = 3


Sub SheetType()
Dim sht As Object
Dim msg As String

  For Each sht In Sheets
    If sht.Type = SheetType_Worksheet Then
      msg = msg & "Sheet:  " & sht.Name & " = Worksheet" & vbCrLf
      If sht.ChartObjects.Count <> 0 Then
        msg = msg & &quot;     Contains &quot; & sht.ChartObjects.Count & &quot; embedded chart(s)&quot; & vbCrLf
      End If
    ElseIf sht.Type = SheetType_Chart Then
      msg = msg & &quot;Sheet:  &quot; & sht.Name & &quot; = Chartsheet&quot; & vbCrLf
    End If
  Next sht
  
  MsgBox msg
  
End Sub


Rob - I ran into the same issue on the constants. Notice that in my construct a chart sheet Type is 3! Go figure.


Regards,
Mike
 
Hallelujah! Goes to show there's always a more straightforward method :) Worth a star in my book.
Rob
[flowerface]
 
Mike,

I used the following code to set-up my chart:
Code:
Worksheets(MySheet).Activate
Charts.Add
ActiveChart.Name = &quot;Results&quot; 
With Charts(&quot;Results&quot;)
  .SetSourceData ...
  .Location Where:=xlLocationAsNewSheet
End With

I guess that by setting the location of the chart to a new sheet means that I have a Chart Sheet rather than an embedded Chart? If however I removed the .Location line in the above code would it mean I have an embedded chart? Clive [infinity]
 
Acron - I just tried your command - just the job! Have a star from me also! Clive [infinity]
 
Clive,

Actually, from what I can tell, the Charts.Add method creates a new chart sheet while the ChartObjects.Add method creates an embedded chart. The Location method is used to move, or re-locate, a chart. So in your example, I don't believe the Location method is doing anything useful (i.e., the new chart is already on a new sheet).

HTH
Mike
 
By embed, do you mean &quot;place on a sheet&quot;? I tried a macro recording of the chart wizard and the only difference between selecting &quot;As new sheet:&quot; and &quot;As object in:&quot; is the
Code:
.Location Where:=xlLocationAsNewSheet
as opposed to
Code:
.Location Where:=xlLocationAsObject
Clive [infinity]
 
Clive,

Yes, embedded = &quot;place on a sheet&quot;. In answering your previous questions I looked at the documentation and tried a coouple of things (but didn't use the Chart Wizard). There's more than one way to skin a cat, and the macro recorder doesn't always generate the most efficient code. If you single-step through the code in your example and stop when the yellow highlight is on the line
Code:
With Charts(&quot;Results&quot;)
then switch from the VBA editor to the Excel view, you will find that a new chart sheet named &quot;Results&quot; has been created. In this case, the Location method is superfluous. The Chart Wizard (or at least the macro recorder code that is generated) apparently always creates a new chart sheet then assigns the user's choice to the Location method. If you are creating a new embedded chart through code, use the ChartObjects.Add method. It gives you control over where on the worksheet to position it. Here's a very simple Example:

Code:
Sub AddEmbeddedChart()
Dim ChObj As ChartObject

  Set ChObj = Sheets(&quot;Sheet1&quot;).ChartObjects.Add(200, 100, 200, 100)
  ChObj.Chart.SetSourceData Source:=Worksheets(&quot;Sheet1&quot;).Range(&quot;B1:B6&quot;), _
     PlotBy:=xlColumns
     
End Sub

Hope this clarifies things.

Regards,
Mike
 
Thanks for the clarification Mike - I understand now - hooray! A star for your patience and explanation! Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top