PC Review


Reply
Thread Tools Rate Thread

Automatically selecting active chart

 
 
Curt
Guest
Posts: n/a
 
      15th Apr 2010
I have the following marco below. I would like to change it so that I do not
have to select an active chart before running it. Instead, I would like it
to always run for the chart in sheet titled "Benchmark Comp Chart". Please
note, there is never more than one chart in that sheet.

Currently, since I have to select an active chart, I have to run this while
my active sheet is "Benchmark Comp Chart". Going forward, I wish to run it
while my active sheet is titled "Misc.".

Sub GetChartValues()
Dim NumberOfRows As Integer
Dim X As Object
Counter = 2

' Calculate the number of rows of data.
NumberOfRows = UBound(ActiveChart.SeriesCollection(1).Values)

Worksheets("Benchmark Comp Chart").Cells(1, 1) = "X Values"

' Write x-axis values to worksheet.
With Worksheets("Benchmark Comp Chart")
.Range(.Cells(2, 1), _
.Cells(NumberOfRows + 1, 1)) = _
Application.Transpose(ActiveChart.SeriesCollection(1).XValues)
End With

' Loop through all series in the chart and write their values to
' the worksheet.
For Each X In ActiveChart.SeriesCollection
Worksheets("Benchmark Comp Chart").Cells(1, Counter) = X.Name

With Worksheets("Benchmark Comp Chart")
.Range(.Cells(2, Counter), _
.Cells(NumberOfRows + 1, Counter)) = _
Application.Transpose(X.Values)
End With

Counter = Counter + 1
Next

End Sub
 
Reply With Quote
 
 
 
 
Wouter HM
Guest
Posts: n/a
 
      15th Apr 2010
Hi Curt,

There is only one line of code needed:

ActiveSheet.ChartObjects.Item(1).Activate

HTH,

Wouter
 
Reply With Quote
 
Curt
Guest
Posts: n/a
 
      16th Apr 2010
Thank you, that worked, but I wish to run this macro from another sheet.

I still have to run this while my active sheet is "Benchmark Comp Chart". I
wish to run it while my active sheet is titled "Misc.".

thanks,

"Wouter HM" wrote:

> Hi Curt,
>
> There is only one line of code needed:
>
> ActiveSheet.ChartObjects.Item(1).Activate
>
> HTH,
>
> Wouter
> .
>

 
Reply With Quote
 
Andy Pope
Guest
Posts: n/a
 
      16th Apr 2010
Hi,

Set a reference to the chart and use that instead of activechart.
In the code I set a reference to the activechart.

Sub GetChartValues()
Dim NumberOfRows As Integer
Dim X As Object
Dim chtTemp As Chart

Set chtTemp = ActiveChart ' or a reference to the required chart
''' Set chtTemp = ActiveSheet.ChartObjects(1).Chart

Counter = 2

' Calculate the number of rows of data.
NumberOfRows = UBound(chtTemp.SeriesCollection(1).Values)

Worksheets("Benchmark Comp Chart").Cells(1, 1) = "X Values"

' Write x-axis values to worksheet.
With Worksheets("Benchmark Comp Chart")
.Range(.Cells(2, 1), _
.Cells(NumberOfRows + 1, 1)) = _
Application.Transpose(chtTemp.SeriesCollection(1).XValues)
End With

' Loop through all series in the chart and write their values to
' the worksheet.
For Each X In chtTemp.SeriesCollection
Worksheets("Benchmark Comp Chart").Cells(1, Counter) = X.Name

With Worksheets("Benchmark Comp Chart")
.Range(.Cells(2, Counter), _
.Cells(NumberOfRows + 1, Counter)) = _
Application.Transpose(X.Values)
End With

Counter = Counter + 1
Next

End Sub


Cheers
Andy

On 15/04/2010 18:23, Curt wrote:
> I have the following marco below. I would like to change it so that I do not
> have to select an active chart before running it. Instead, I would like it
> to always run for the chart in sheet titled "Benchmark Comp Chart". Please
> note, there is never more than one chart in that sheet.
>
> Currently, since I have to select an active chart, I have to run this while
> my active sheet is "Benchmark Comp Chart". Going forward, I wish to run it
> while my active sheet is titled "Misc.".
>
> Sub GetChartValues()
> Dim NumberOfRows As Integer
> Dim X As Object
> Counter = 2
>
> ' Calculate the number of rows of data.
> NumberOfRows = UBound(ActiveChart.SeriesCollection(1).Values)
>
> Worksheets("Benchmark Comp Chart").Cells(1, 1) = "X Values"
>
> ' Write x-axis values to worksheet.
> With Worksheets("Benchmark Comp Chart")
> .Range(.Cells(2, 1), _
> .Cells(NumberOfRows + 1, 1)) = _
> Application.Transpose(ActiveChart.SeriesCollection(1).XValues)
> End With
>
> ' Loop through all series in the chart and write their values to
> ' the worksheet.
> For Each X In ActiveChart.SeriesCollection
> Worksheets("Benchmark Comp Chart").Cells(1, Counter) = X.Name
>
> With Worksheets("Benchmark Comp Chart")
> .Range(.Cells(2, Counter), _
> .Cells(NumberOfRows + 1, Counter)) = _
> Application.Transpose(X.Values)
> End With
>
> Counter = Counter + 1
> Next
>
> End Sub


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

 
Reply With Quote
 
Curt
Guest
Posts: n/a
 
      16th Apr 2010
I just recorded a macro that did the rest. I am all set now. Thanks

"Curt" wrote:

> Thank you, that worked, but I wish to run this macro from another sheet.
>
> I still have to run this while my active sheet is "Benchmark Comp Chart". I
> wish to run it while my active sheet is titled "Misc.".
>
> thanks,
>
> "Wouter HM" wrote:
>
> > Hi Curt,
> >
> > There is only one line of code needed:
> >
> > ActiveSheet.ChartObjects.Item(1).Activate
> >
> > HTH,
> >
> > Wouter
> > .
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Worksheets from active to end Craig Microsoft Excel Programming 2 10th Sep 2009 06:49 PM
Selecting cells on active row merry_fay Microsoft Excel Misc 0 26th May 2009 05:16 PM
Selecting the active row el_grimley Microsoft Excel Programming 2 5th Aug 2005 09:19 AM
Selecting the Active Column dolphinv4 Microsoft Excel Misc 1 16th Aug 2004 06:03 AM
automatically create chart upon selecting Jason Microsoft Excel Charting 6 3rd Nov 2003 09:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:26 PM.