Create charts with data from a different worksheet and varying number of columns

Asked

Viewed 31 times

0

I need to generate a graph for each line of a database. This database varies both in number of columns and number of rows, and is in a different spreadsheet, chanada Matrix 1. Whenever I run my code is giving an error in Setsourcedata, and I can not identify what is happening.

Can anyone help me? Follows my code.

Sub createColumnChartMatriz12()

Dim ChartName As String
Dim Row As Integer
Dim ChartRow As Integer
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Matriz 1")
Dim k As Long
Dim z As Long

k = sh.Range("A1", 
sh.Range("A1").End(xlDown)).Rows.Coun
z = sh.Cells(1, sh.Columns.Count).End(xlToLeft).Column - 4
ThisWorkbook.Sheets("Matriz1Chart").Select
Cells.Select
Selection.RowHeight = 15.5
Cells(1, 1).Select


ChartRow = 49
Row = 2
For Row = 2 To k

ChartName = "Utilização no Período " & sh.Cells(Row, 1).Value

ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select

With ActiveChart
    .SetSourceData Source:=sh.Range(Cells(Row, 4), Cells(Row, z)), _
            PlotBy:=xlRows
    .FullSeriesCollection(1).XValues = "='Matriz 1'!$D$1:$AM$1"
    .Parent.Height = Range("A1:A15").Height
    .Parent.Width = Range("A1:J1").Width
    .Parent.Top = Range("A" & ChartRow).Top
    .Parent.Left = Range("A" & ChartRow).Left
    .HasTitle = True
    .ChartTitle.Text = ChartName
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Meses"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Utilização"
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "mm-yyyy"
End With

ChartRow = ChartRow + 16

Next

End Sub
  • Can you create a [mcve]? with the data table where this occurs?

  • Try .SetSourceData Source:=sh.Range(sh.Cells(Row, 4), sh.Cells(Row, z)), _
 PlotBy:=xlRows

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.