无法在vba图表上操作seriescollection

时间:2017-04-10 17:32:44

标签: vba excel-vba excel

在msdn(这里,特别是https://msdn.microsoft.com/en-us/library/office/ff821866.aspx)上有些过时的东西,还是我真的很蠢?我有一些代码:

'Cel is a range, CelCol is a long, i, j, k, l are all long, LastColumn is a long, GraphDataStationBlock is a constant (long), wsh1 and wsh2 are worksheets, and chrt is the chart
'option explicit is on so if I missed mentioning a variable, it *was* declared, I just missed it
'I tried setting the source data both before and after all this just in case it mattered but nothing changed
j = 1
For Each Cel In wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 1, 1), wsh1.Cells(GraphDataStationBlock * (i + 1), 1)).Cells
    If Cel.Offset(0, 1) <> vbNullString Then
        wsh1.Cells(Cel.Row, CelCol) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(Cel.Row, 3), wsh2.Cells(Cel.Row, 26)))
        chrt.SeriesCollection(j).XValues = wsh1.Range("B3:B5") 'all but straight from the msdn website, still doesn't work! 
        'I also tried a standard range(cell1, cell2) format (not letter/number) in case that would work but it did not, even though msdn says ranges should be fine
        chrt.SeriesCollection(j).name = wsh1.Cells(Cel.Row, 1) & vbSpace & wsh1.Cells(Cel.Row, 2)
        'always gives "unable to get name property of the series class"
        j = j + 1
    End If
Next Cel
'there's a lot more besides this of course but this is just the problematic part

长话短说,它从表A复制一行最大值(表示一天的每小时数据),将其放入表B上的相应列(表示该月的每一天),然后将其映射到图表C或者它应该是。在实践中,它复制了最大值,然后我得到了无数系列的运行时错误1004,对于seriescollection的名称和XValues部分。

我还没有真正构建那么多的图表 - 也就是说,没有 - 所以我有点浮躁了,如果有更好的制作图表的方法那么我就全都是耳朵,但不然......

编辑:它从中得到的数据非常简单 - 跨越顶部的日期数字(1到最后一天),左侧两列中的标签,然后是每天填写的数据。想想看,在任何特定时间都有相当数量的源数据是空的,对吗?

此外,更多代码。这仍然不是整个程序,但它涵盖了比上述循环更多的内容。

Option Explicit
'vbSpace will be mentioned, I just saved it as a public variable equalling " " because I find it easier to type
Sub UpdateMonthlyGraphData(ByVal wsh2 As Worksheet, ByVal Yesterdate As Date, ByVal FirstDate As Date, ByVal LastDate As Date)
'wsh2 has the daily information
With Excel.Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With


Dim LastColumn As Long, i As Long, j As Long, SheetCount As Long, CelCol As Long
Dim FirstDay As Long, LastDay As Long, FirstWeekday As Long
Dim Yesteryear As Long, Yestermonth As Long, Yesterday As Long
Dim FormattedMonth As String, ChartType As String
Dim Cel As Range
Dim SerCol As Series
Dim wsh1 As Worksheet
Dim wb1 As Workbook
Dim chrt As Chart

Set wb1 = ThisWorkbook
FirstWeekday = Weekday(FirstDate, vbSunday)
LastDay = Day(LastDate)
FormattedMonth = Format(Yesterdate, "MMM YYYY")
SheetCount = wb1.Sheets.Count
Yesteryear = Year(Yesterdate)
Yestermonth = Month(Yesterdate)
Yesterday = Day(Yesterdate)

LastColumn = 2 + LastDay     'Set the data range to the appropriate size according the number of days in the month

If Not CBool(WorksheetExists(MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data")) Then
'WorksheetExists just looks thruogh each worksheet and either returns the index of the sheet named (if it exists) or zero (if not).  
    SheetCount = wb1.Sheets.Count       'Monthly Data sheet creation
    wb1.Worksheets("Template Monthly Graph Data").Copy after:=wb1.Sheets(SheetCount)
    SheetCount = SheetCount + 1
    Set wsh1 = wb1.Sheets(SheetCount)
    wsh1.Move after:=wb1.Worksheets("Template WOT Main")
    wsh1.name = MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data"
    LastDay = Day(DateSerial(Yesteryear, Yestermonth + 1, 0))
    For i = 1 To 31     'only sort of tested code, be sure to check in on it to make sure it works properly
        If i <= LastDay Then
            wsh1.Cells(2, i + 2) = i & " : " & WeekdayName(Weekday(DateSerial(Yesteryear, Yestermonth, i), vbSunday), True)
        Else
            wsh1.Cells(2, i + 2) = "N/A"
        End If
    Next i
Else
    Set wsh1 = wb1.Worksheets(MonthName(Yestermonth, True) & vbSpace & Yesteryear & vbSpace & "Monthly Graph Data")
    LastDay = DateSerial(Yesteryear, Yestermonth + 1, 0)
End If

Set Cel = wsh1.Range(wsh1.Cells(2, 3), wsh1.Cells(2, LastDay + 2)).Cells.Find(Yesterday & " : " & WeekdayName(Weekday(Yesterdate, vbSunday), True))
        'importing yesterday's data
If Not Cel Is Nothing Then
    Set Cel = wsh1.Cells(GraphDataStationBlock - 3, Cel.Column)
    CelCol = Cel.Column
    Cel = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(GraphDataStationBlock - 3, 3), wsh2.Cells(GraphDataStationBlock - 3, 26)))
    Cel.Offset(1, 0) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(GraphDataStationBlock - 2, 3), wsh2.Cells(GraphDataStationBlock - 2, 26)))
    wsh1.Range(Cel, Cel.Offset(1, 0)).NumberFormat = "0.0"
Else
    MsgBox "Monthly Graph Data Sheet did not initialize correctly.  Please review code and results."
    Exit Sub        'just in case?
End If

For i = 0 To 2
    Select Case i
        Case 0
            ChartType = "Winding"
        Case 1
            ChartType = "Oil"
        Case 2
            ChartType = "MW"
    End Select

    If Not CBool(ChartExists(FormattedMonth & " Monthly " & ChartType & " Graph")) Then 'the chart counterpart to the above "WorksheetExists" 
        wb1.Charts("Template Monthly " & ChartType & " Graph").Copy after:=wb1.Sheets(SheetCount)
        SheetCount = SheetCount + 1
        Set chrt = wb1.Sheets(SheetCount)
        chrt.name = FormattedMonth & " Monthly " & ChartType & " Graph"
        chrt.Move before:=wb1.Worksheets(FormattedMonth & " Monthly Graph Data")
        chrt.Legend.Font.Size = 10  'was there before, keep it I guess?
        If i < 2 Then
            chrt.ChartTitle.Characters.Text = FormattedMonth & vbSpace & ChartType & " Temp Peaks"
        Else
            chrt.ChartTitle = FormattedMonth & vbSpace & ChartType & " Peaks"
        End If
    Else
        Set chrt = wb1.Charts(FormattedMonth & " Monthly " & ChartType & " Graph")
    End If

    chrt.SetSourceData Source:=Union(wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 3, 3), wsh1.Cells(GraphDataStationBlock * i + 2 + Feeders138, LastColumn)), wsh1.Range(wsh1.Cells(GraphDataStationBlock * (i + 1) - Feeders416 - 4, 3), wsh1.Cells(GraphDataStationBlock * (i + 1) - 5, LastColumn))), PlotBy:=xlRows

    For Each SerCol In chrt.SeriesCollection
        Debug.Print SerCol.ChartType 'this didn't work
        Stop 'it'd be convenient if it did, though, and if I can get the code to work at all, I will probably try and make it look all pretty and compact like this
    Next SerCol

    j = 1
    For Each Cel In wsh1.Range(wsh1.Cells(GraphDataStationBlock * i + 1, 1), wsh1.Cells(GraphDataStationBlock * (i + 1), 1)).Cells
        If Cel.Offset(0, 1) <> vbNullString Then
            wsh1.Cells(Cel.Row, CelCol) = WorksheetFunction.Max(wsh2.Range(wsh2.Cells(Cel.Row, 3), wsh2.Cells(Cel.Row, 26)))
                    'noted limitation: as is this requires that the order of the feeders in the monthly and daily graph data sheets be structured the same way
            chrt.SeriesCollection(j).XValues = wsh1.Range("B3:B5") '"='" & wsh1.name & "'!" & wsh1.Range(wsh1.Cells(2, 3), wsh1.Cells(2, LastColumn)).Address '"='" & Yesterdate & " Graph Data'!R2C3:R2C26"
            chrt.SeriesCollection(j).name = wsh1.Cells(Cel.Row, 1) & vbSpace & wsh1.Cells(Cel.Row, 2)
            j = j + 1
        End If
    Next Cel

Next i

0 个答案:

没有答案