charts and macros

smokenack

Active Member
Joined
Dec 12, 2003
Messages
351
I've been trying to set up a workbook where each sheet represents one day of the month and will display the number of staff unavailable to work that day as a percentage displayed on a pie chart. I've no problem with doing that but I would like to display the chart by clicking a control command button. The total number of staff are entered in A1 and the total number sick is entered into B1.

I created the chart on one sheet and recorded my actions using the macro recorder with the intention of assigning this macro to the button.

Sub pie6()

Range("A1:B1").Select
Charts.Add
ActiveChart.ChartType = xl3DPieExploded
ActiveChart.SetSourceData Source:=Sheets("Sheet5").Range("A1:B1"), PlotBy:= _
xlRows
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet5"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = _
"% of productive staff not available due to sickness"
End With
End Sub


Two problems have arisen.

1/ I set the data labels to percentage but the macro recorder does not record this.

2/I need this to refer to the active sheet not Sheet5 and have been unable to adjust the code accordingly.

thanks for looking

Nick
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Nick, try sumpin' like so...
Code:
Sub Quickie1()
    
    Dim chtNew As Chart, wsActive As Worksheet
    
    Set wsActive = ActiveSheet
    Set chtNew = Charts.Add
    
    With chtNew
        .ChartType = xl3DPieExploded
        .SetSourceData Source:=wsActive.Range("A1:B2"), PlotBy:=xlRows
        .HasTitle = True
        .ChartTitle.Characters.Text = "% of prod staff"
        .Location Where:=xlLocationAsObject, Name:=wsActive.Name
    End With
End Sub
Tweak and play with it to yer heart's content...

<sup>edit</sup> For the percent thing, you can add a line inside the with/end with like so...
Code:
.ApplyDataLabels xlDataLabelsShowPercent, True
Note that things go a bit wonky once you change the location (it's the ChartObject versus Chart thing) so it just makes coding simpler to put anything you wanna do before you move the chart onto the worksheet... <sub>/edit</sub>

HTH
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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