【问题标题】:Creating charts in Excel VBA - Missing Labels在 Excel VBA 中创建图表 - 缺少标签
【发布时间】:2015-10-09 12:27:39
【问题描述】:

更新 2: 试图用 VBA 中的列范围定义变量。 谁能猜出这段代码有什么问题? 提前谢谢...


更新 1:我已成功生成图表。感谢你们的建设性批评。 我是 VBA 的新手,正在学习 :) 我现在的挑战是将为图表选择的行定义为变量。

即。用户为 ROW 提供输入,宏会为预期的 Row 生成图表。

在下方找到更新后的代码。 谢谢大家


我需要编写一个宏来在 Excel 中创建一个单独的性能图表。我记录了几行代码,但生成的图表在 X 和 Y 轴上没有任何标签。

我的要求是创建一个具有以下功能的图表:

  1. 选择行号的选项。在宏的开头(需要为哪一行准备图表) - 一些输入框
  2. 比较第 1 行和第 2 行的比较功能。(某些输入框)
  3. 数据系列标签(X 轴)
  4. 图表标题

我的 Excel 看起来像这样:

Sales Achieved  |Clients Met|   Client Responsiveness|  

Employee 1 |           6    | 7         |            8           |

Employee 2 |           6    | 7         |            8           |

Employee 3 |           6    | 7         |            8           |

Employee 4 |           6    | 7         |            8           |

Sub generatecharts()
Dim xValRange As Range
Dim r
r = irow
irow = InputBox("Which Chart do you want to generate?")

With ActiveSheet
Set xValRange = ActiveSheet.Range("B" & r & ":" & "Q" & r)
End With

With ActiveSheet
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Values = xValRange
ActiveChart.SeriesCollection(1).XValues = "=Sheet2!$B$1:$Q$2"
ActiveChart.SeriesCollection(1).Name = "=Sheet2!$A$" & r
With ActiveChart.Parent
.Height = 400
.Width = 800
End With
End With
End Sub

【问题讨论】:

  • 这读起来更像是博客条目的开始。你有什么问题?
  • Hi Sous,我的要求是使用具有上述功能的 MACRO 创建一个图表。
  • 是的,我明白了。不幸的是,这并不是真正的代码编写服务。最好分解你的需求,尝试解决每一个需求。如果你对某事感到难过,那么就来这里提出一个具体的问题,你尝试了什么,什么没用,预期的输出是什么......等等。通常情况下,“这里为我做这个”类型的帖子不太受欢迎。
  • 大家好...代码现在运行良好,符合预期:)

标签: excel charts vba


【解决方案1】:
Sub Macro4()
Dim xValRange As Range
Dim r As Integer
Range("A30").Select 'selected a blank cell on purpose to avoid undefined charts
r = InputBox("Enter the Row Number to generate Chart")
With ActiveSheet

Set xValRange = ActiveSheet.Range("$B$" & r & ":" & "$T$" & r)
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = Cells(r, 1)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Values = xValRange
ActiveChart.SeriesCollection(1).XValues_
=ActiveSheet.Range("=KRAs!$B$1:$T$2")
ActiveChart.SeriesCollection(1).Name = ActiveSheet.Range("=KRAs!$A$" & r)
ActiveChart.SetElement (msoElementDataLabelInsideEnd) 'to add the data labels
End With
With ActiveChart.Parent
.Height = 400
.Width = 800
.Top = 150    ' reposition
.Left = 200   'reposition
End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-30
    • 2021-11-03
    • 2018-04-18
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多