Home » excel » 2nd Alternative of stacked bar chart in excel vba

2nd Alternative of stacked bar chart in excel vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to get the excel stacked bar chart in vba but when i try my result gets varied?

what am i doing wrong?

My expected result and Result I get:
Chart

VBA Code for your reference:

Sub createchart()
 Dim ws As Worksheet
 Set ws = ThisWorkbook.Sheets("High")

 Dim chrt As Object
  Set chrt = ws.Shapes.AddChart2(297, xlBarStacked)

  With chrt.Chart
   .SetSourceData Source:=ws.Range("E4:E7")
   .HasTitle = True
   .ChartTitle.Text = ws.Range("E3").Value
 End With


 End Sub
How to&Answers:

You need to plot by rows to get your desired output. You can try the modifications below.

Sub createchart()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("High")

    Dim chrt As Object
    Set chrt = ws.Shapes.AddChart

    With chrt.Chart
       .SetSourceData Source:=ws.Range("E4:E7"), PlotBy:=xlRows
       .HasTitle = True
       .ChartTitle.Text = ws.Range("E3").Value
       .ChartType = xlBarStacked
    End With


End Sub