首页 文章

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

提问于
浏览
0

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


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

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

找到下面的更新代码 . 谢谢大家


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

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

  • 选择行号的选项 . 在宏的开头(图表需要准备哪一行) - 一些输入框

  • 比较功能比较第1行和第2行 . (某些输入框)

  • 数据系列标签(X轴)

  • 图表 Headers

我的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
    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
    

相关问题