2

SO社区,

我希望使用 VBA 自动化我在工作中使用的一些指标。我目前正在尝试读取存储了票务原始数据的数组,然后将该值存储为小数或百分比。将其存储在数组中后,我尝试使用数组创建或更新图表系列,并将该值显示为百分比。我怀疑我只是缺少一些语法,但我检查了 SO、MSDN 和 Excel 帮助,但没有运气。我在下面附上了相关代码:

功能

Function calcTopApplications(iArray As Variant)

Dim m_counter As Long, r_counter As Long, placeholder As Double
Dim fNav_counter As Long, pmoNav_counter As Long, rmgr_counter As Long, wlm_counter As Long, total_counter As Long
ReDim tkt_month_arr(12), tkt_fnav_arr(12)

For m_counter = 1 To 12
    fNav_counter = 0
    pmoNav_counter = 0
    rmgr_counter = 0
    wlm_counter = 0
    total_counter = 0
    For r_counter = 2 To UBound(iArray, 1)
        If iArray(r_counter, 1) <> iArray(r_counter - 1, 1) Then
            If CDate(iArray(r_counter, 5)) >= DateAdd("m", -m_counter, DateSerial(Year(Date), Month(Date), 1)) Then
                If CDate(iArray(r_counter, 5)) < DateAdd("m", (1 - m_counter), DateSerial(Year(Date), Month(Date), 1)) Then
                total_counter = total_counter + 1
                    If StrConv(iArray(r_counter, 7), vbLowerCase) = "franchise navigator" Then
                        fNav_counter = fNav_counter + 1
                    End If
                End If
            End If
        End If
    Next r_counter
    placeholder = FormatNumber(fNav_counter / total_counter, 2)
    tkt_month_arr(12 - (m_counter - 1)) = CLng(DateAdd("m", -m_counter, DateSerial(Year(Date), Month(Date), 1)))
    tkt_fnav_arr(12 - (m_counter - 1)) = placeholder
Next m_counter

End Function

子程序

If Me.ChartObjects("Top 4 Applications Ticket Volume") Is Nothing Then
    On Error GoTo 0
    With Me.Shapes.AddChart
        .Left = Me.Range("A16").Left
        .Top = Me.Range("A16").Top
        .Width = Me.Range("A16:S16").Width
        .Height = Me.Range("A16:A30").Height
        .Select
    End With
    With ActiveChart
        .ChartType = xlLine
        .ChartStyle = 42
        .HasDataTable = True
        .HasTitle = True
        .Parent.Name = "Top 4 Applications Ticket Volume"
        .ChartTitle.Caption = "Open/Close Ticket Volume by Month (Top 4 Applications)"
        .Axes(xlCategory).TickLabels.NumberFormat = "mmm yyyy"
    End With
    With ActiveChart.SeriesCollection
        .NewSeries
        .Item(1).Name = "Franchise Navigator"
        .Item(1).XValues = tkt_month_arr
        .Item(1).Values = tkt_fnav_arr
    End With
Else
    With Me.ChartObjects("Top 4 Applications Ticket Volume")
        .Select
    End With
    With ActiveChart.SeriesCollection
        .Item(1).XValues = tkt_month_arr
        .Item(1).Values = tkt_fnav_arr
    End With
End If

这给了我百分比的值,但在图表上不表现为百分比(有 % 符号)。

4

1 回答 1

0

当您“正常”创建图表并添加数据表时,您所要做的就是格式化源单元格,然后表格如下。但是您使用的方法意味着您没有“源单元格”;据我所知,微软没有提供一种方法来修改使用您的方法生成的数据表的格式。

一个“丑陋的解决方法”是创建一个隐藏的工作表,将数据放在那里,并正确格式化。创建数据表时指向此数据,格式将正确。

如果 Microsoft 提供您所要求的灵活性,那就太好了……叹息

于 2013-06-17T17:48:03.023 回答