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
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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
 
Upvote 0
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?
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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