从Matlab导入的动态数组

时间:2018-09-17 09:37:43

标签: excel vba

我正在用excel准备一个模板,该模板将直接写入Matlab中的数据。

我有来自Matlab的动态数据,但是绘制该列的列是固定的。 enter image description here

要在y轴上绘制的数据应从列“ H”到“ J”,并且要在x轴上绘制的数据应为“ F”和“ G”。在这里,数据是动态的,并且根据Matlab中的分析可以更多。

问题是:我编写的VBA代码没有绘制图表。

代码是:

Sub Chart()

    Dim rngDataSource As Range
    Dim RowsCt As Long
    Dim ColsCt As Integer
    Dim Counter As Integer
    Dim chtChart As Chart
    Dim Newsrs As Series
    Dim LastRow As Long

    With ActiveSheet
        LastRow = .Range("G" & .Rows.Count).End(xlUp).Row
        Set rngDataSource = .Range(.Cells(1, 7), .Cells(LastRow, 14)) ' & LastRow & ", H1:N" & LastRow)
        ShName = .Name
    End With

    With rngDataSource
        RowsCt = .Rows.Count
        ColsCt = .Columns.Count
    End With

    '' Create the chart
    Set chtChart = ActiveSheet.ChartObjects.Add( _
        Left:=ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + _
            ActiveWindow.Width / 4, _
        Width:=ActiveWindow.Width / 2, _
        Top:=ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + _
            ActiveWindow.Height / 4, _
        Height:=ActiveWindow.Height / 2).Chart

    With chtChart
        .ChartType = xlXYScatterLines

        '' Remove any series created with the chart
        Do Until .SeriesCollection.Count = 0
            .SeriesCollection(1).Delete
        Loop

        For Counter = 2 To ColsCt
            '' Add each series
            Set Newsrs = .SeriesCollection.NewSeries
            With Newsrs
                .Name = rngDataSource.Cells(1, Counter)
                .Values = rngDataSource.Cells(2, Counter) _
                    .Resize(RowsCt - 1, 1)
                .XValues = rngDataSource.Cells(2, 1) _
                    .Resize(RowsCt - 1, 1)
            End With
        Next
    End With
End Sub

0 个答案:

没有答案