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