Arunkumar
Arunkumar

Reputation: 13

2nd Alternative of stacked bar chart in excel vba

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

Upvotes: 0

Views: 220

Answers (1)

Darrell H
Darrell H

Reputation: 1886

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

Upvotes: 3

Related Questions