在Excel VBA中创建图表 - 缺少标签

时间:2015-10-09 12:27:40

标签: excel-vba charts vba excel

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

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

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

找到下面的更新代码。 感谢所有

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

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

  1. 选择行号的选项。在宏的开头(图表需要准备哪一行) - 一些输入框
  2. 用于比较第1行和第2行的比较功能(某些输入框)
  3. 数据系列标签(X轴)
  4. 图表标题
  5. 我的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
    

1 个答案:

答案 0 :(得分:0)

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