Please help me, I am writing an excel vba chart1 code.

Quan71

New Member
Joined
Dec 10, 2022
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Can you please help me write an excel VBA code? Currently, it's a stacked chart, but there's one "defect rate" that needs to be converted to a line chart, and I haven't been successful. How can I make the "defect rate" line chart appear?

VBA Code:
Sub CreateStackedChart()
Dim ws As Worksheet
Dim cht As Chart
Dim co As ChartObject
Dim rng As Range
Dim i As Integer
Set ws = ThisWorkbook.Sheets("SHEET2")
Set co = ws.ChartObjects.Add(Left:=50, Width:=600, Top:=50, Height:=300)
Set cht = co.Chart
cht.ChartType = xlColumnStacked
cht.SetSourceData ws.Range("B3:n3"), xlColumns
Dim seriesIndex As Integer
seriesIndex = 1
For i = 4 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    If Not IsEmpty(ws.Range("A" & i)) Then
        cht.SeriesCollection.NewSeries
        cht.SeriesCollection(seriesIndex).Name = ws.Range("A" & i)
        Set rng = ws.Range(ws.Range("B" & i), ws.Range("M" & i))
        cht.SeriesCollection(seriesIndex).Values = rng
        If ws.Range("A" & i) = "不良率" Then
            cht.SeriesCollection(seriesIndex).ChartType = xlLine
            cht.SeriesCollection(seriesIndex).AxisGroup = xlSecondary
            cht.SeriesCollection(seriesIndex).HasDataLabels = True
            cht.SeriesCollection(seriesIndex).DataLabels.NumberFormat = "0.0%"
        End If
        seriesIndex = seriesIndex + 1
    Else
    End If
Next i
End Sub




1.JPG
2.JPG
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,223,948
Messages
6,175,571
Members
452,652
Latest member
eduedu

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