使用 vba 在堆积柱形图上显示数据值
Display Data values on the stacked column chart using vba
我有一个数据 table,其中图表的第 15 行有月份,"B17:B25" 的 "B" 列中有各种公司名称。示例 table 如下图所示
现在我编写了一段代码,它将捕获这些值并输出一个堆积柱形图,如下所示:
这是我写的代码:
Sub getchart()
y = Format(Now, "ww")
Dim ws As Worksheet
Dim aCell As Range, Rng As Range, bCell As Range, sRng As Range, fRng As Range
Dim col As Long, lRow As Long, srow As Long
Dim colName As String
Dim wsTemp As Worksheet
Dim oChrt As ChartObject
Dim sheetname As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Calculation")
With ws
Set aCell = .Range("D16:BC16").Find(What:=y, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set bCell = .Range("B:B").Find(What:="Total no of Consultants", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
col = aCell.Column
colName = Split(.Cells(, col).Address, "$")(1)
Else
MsgBox "Nov Not Found"
End If
If Not bCell Is Nothing Then
srow = bCell.Row
x = srow - 1
Else
MsgBox "Nov Not Found"
End If
Set sRng = .Range(colName & "17:" & colName & x)
Debug.Print sRng.Address
Set fRng = .Range("B" & "17:" & "B" & x)
Debug.Print fRng.Address
End With
'~~> Set the sheet where you have the charts data
ActiveWorkbook.Worksheets("Calculation").Activate
'~~> This is your charts range
Set Rng = Range("D17:G25")
'~~> Delete the temp sheeet if it is there
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("TempOutput").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'~~> Add a new temp sheet
Set wsTemp = ThisWorkbook.Sheets.Add
With wsTemp
'~~> Give it a name so that we can delete it as shown above
'~~> This is just a precaution in case `wsTemp.Delete` fails below
.Name = "TempOutput"
'~~~> Add the chart
Set oChrt = .ChartObjects.Add _
(Left:=5, Width:=650, Top:=20, Height:=350)
'~~> Set the chart's source data and type
'~~> Change as applicable
With oChrt.Chart
.SetSourceData Source:=Rng
.ChartType = xlColumnStacked
.HasTitle = True
'.Legend.LegendEntries (fRng)
.HasLegend = True
.ChartTitle.Text = "Total # Consultants and Spread"
' .SeriesCollection(1).DataLabels.ShowValues = True
For intSeries = 1 To .SeriesCollection.Count
.SeriesCollection(intSeries).Name = fRng.Cells(intSeries, 1)
Next
End With
End With
End Sub
现在我想要的是我必须在堆积柱形图的列中显示每个条形的数据值。那么如何编写 vba 代码来显示数据值。
请帮我解决这个问题
您可以轻松录制宏,select 系列并向系列添加数据标签。这将为您提供所需的代码,您可以根据需要对其进行调整。
当堆叠的列数据点太小而无法实际显示数据标签时,就会出现真正的问题。在堆积柱形图中,数据标签只能显示在数据点内(即代表数据的彩色矩形)。
在开始自动创建数据标签之前,请复制一份文件并手动添加数据标签。然后仔细查看用户体验:在非常窄的数据点上使用数据标签的图表是什么样的?查看您的屏幕截图,如果您将数据标签添加到 X 位置 1、5 和 6 的列 -- 那看起来如何?
你会发现答案是:不漂亮。 -- 根据你的屏幕截图,你甚至没有绘制 table 中的所有数据。
所以,您真的想回到绘图板并计划更好的数据可视化。因为你现在的图表不能很好地传达。
如果您想了解一流的数据可视化,请获取任何 Stephen Few book. If you want to apply the principles of good data viz in Excel, take a look at Jon Peltier's site,其中详细解释了可以使用 Excel 图表完成的任何操作。
这不是您想听到的答案,但坦率地说,我不知道如何为列中的这些微小数据点添加数据标签,并使其易于理解 reader图表。
我有一个数据 table,其中图表的第 15 行有月份,"B17:B25" 的 "B" 列中有各种公司名称。示例 table 如下图所示
现在我编写了一段代码,它将捕获这些值并输出一个堆积柱形图,如下所示:
这是我写的代码:
Sub getchart()
y = Format(Now, "ww")
Dim ws As Worksheet
Dim aCell As Range, Rng As Range, bCell As Range, sRng As Range, fRng As Range
Dim col As Long, lRow As Long, srow As Long
Dim colName As String
Dim wsTemp As Worksheet
Dim oChrt As ChartObject
Dim sheetname As String
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Calculation")
With ws
Set aCell = .Range("D16:BC16").Find(What:=y, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set bCell = .Range("B:B").Find(What:="Total no of Consultants", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
'~~> If Found
If Not aCell Is Nothing Then
col = aCell.Column
colName = Split(.Cells(, col).Address, "$")(1)
Else
MsgBox "Nov Not Found"
End If
If Not bCell Is Nothing Then
srow = bCell.Row
x = srow - 1
Else
MsgBox "Nov Not Found"
End If
Set sRng = .Range(colName & "17:" & colName & x)
Debug.Print sRng.Address
Set fRng = .Range("B" & "17:" & "B" & x)
Debug.Print fRng.Address
End With
'~~> Set the sheet where you have the charts data
ActiveWorkbook.Worksheets("Calculation").Activate
'~~> This is your charts range
Set Rng = Range("D17:G25")
'~~> Delete the temp sheeet if it is there
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("TempOutput").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'~~> Add a new temp sheet
Set wsTemp = ThisWorkbook.Sheets.Add
With wsTemp
'~~> Give it a name so that we can delete it as shown above
'~~> This is just a precaution in case `wsTemp.Delete` fails below
.Name = "TempOutput"
'~~~> Add the chart
Set oChrt = .ChartObjects.Add _
(Left:=5, Width:=650, Top:=20, Height:=350)
'~~> Set the chart's source data and type
'~~> Change as applicable
With oChrt.Chart
.SetSourceData Source:=Rng
.ChartType = xlColumnStacked
.HasTitle = True
'.Legend.LegendEntries (fRng)
.HasLegend = True
.ChartTitle.Text = "Total # Consultants and Spread"
' .SeriesCollection(1).DataLabels.ShowValues = True
For intSeries = 1 To .SeriesCollection.Count
.SeriesCollection(intSeries).Name = fRng.Cells(intSeries, 1)
Next
End With
End With
End Sub
现在我想要的是我必须在堆积柱形图的列中显示每个条形的数据值。那么如何编写 vba 代码来显示数据值。
请帮我解决这个问题
您可以轻松录制宏,select 系列并向系列添加数据标签。这将为您提供所需的代码,您可以根据需要对其进行调整。
当堆叠的列数据点太小而无法实际显示数据标签时,就会出现真正的问题。在堆积柱形图中,数据标签只能显示在数据点内(即代表数据的彩色矩形)。
在开始自动创建数据标签之前,请复制一份文件并手动添加数据标签。然后仔细查看用户体验:在非常窄的数据点上使用数据标签的图表是什么样的?查看您的屏幕截图,如果您将数据标签添加到 X 位置 1、5 和 6 的列 -- 那看起来如何?
你会发现答案是:不漂亮。 -- 根据你的屏幕截图,你甚至没有绘制 table 中的所有数据。
所以,您真的想回到绘图板并计划更好的数据可视化。因为你现在的图表不能很好地传达。
如果您想了解一流的数据可视化,请获取任何 Stephen Few book. If you want to apply the principles of good data viz in Excel, take a look at Jon Peltier's site,其中详细解释了可以使用 Excel 图表完成的任何操作。
这不是您想听到的答案,但坦率地说,我不知道如何为列中的这些微小数据点添加数据标签,并使其易于理解 reader图表。