Macro to loop through each row and create a 3d stacked bar graph per row

TMan1980

New Member
Joined
Mar 25, 2019
Messages
4
Hi all,

I have created my Do Loop to loop through my excel spreadsheet by row but I am stuck on creating a 3D stacked bar chart for each row,

here is sample data

Name Salary Bonus Other Chart

a 50 30 20
b 60 40 25
c 35 15 10


I would like each row to automatically create a chart by clicking the assigned macro button

Any help would be appreciated
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,143
this may help

Code:
Dim myRange As Range
r = ActiveCell.Row
Set myRange = Range("A" & r & ":D" & r)
    ActiveSheet.Shapes.AddChart2(286, xl3DColumnStacked).Select
    ActiveChart.SetSourceData Source:=myRange

Ross
 

TMan1980

New Member
Joined
Mar 25, 2019
Messages
4
Ross,

This looks like it definitely adds the chart.. is there a way to get each chart to fit inside column d and the row it corresponds too?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
Assuming that the active sheet contains the data, and that the data is located in Columns A through D, and that the corresponding chart for each row is to be created in Column E, try...

Code:
Option Explicit

Sub Create3DStackedBarCharts()


    Dim currentChart As Chart
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastRow
        Set currentChart = ActiveSheet.Shapes.AddChart2(XlChartType:=xl3DBarStacked, Left:=Cells(i, "E").Left, Top:=Cells(i, "E").Top, Width:=Cells(i, "E").Width, Height:=Cells(i, "E").Height).Chart
        With currentChart
            .SetSourceData Range("A" & i & ":D" & i)
            .PlotBy = xlColumns
        End With
    Next i
    
    Set currentChart = Nothing
    
End Sub

Hope this helps!
 

TMan1980

New Member
Joined
Mar 25, 2019
Messages
4
Domenic,

That works like a charm. Thank you. definitely learned something new here.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,514
Messages
5,602,089
Members
414,501
Latest member
mdhaumyu

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
Top