创建与 excel 个单元格相关的图表
Creating a chart related to excel cell
我对 VBA 完全陌生。尝试将以下代码放在一起:存在具有多种风险的数据集。用户单击命令按钮,弹出一条消息 "please select a risk to be graphed",用户从数据库中选择一个风险(E 列中的一个)。之后一切都自动化了:插入散点图,选择三个 x 轴值(它们在与所选风险相同的行中的 J、K、L 列中),y 轴的三个值是 0 , 列 P 中的值与 rist 相同,0。绘制图形并设置格式后。格式化非常简单,到目前为止我还停留在第一部分。这是我到目前为止所拥有的。错误出现在行 selects x-values, "the element with this name was not found" 上。该行标记为 **
Sub Makro2()
Dim rng As Range
Dim InputRng As Range, OutRng As Range
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Choose the risk to draw", xTitleId, InputRng.Address, Type:=8)
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.SeriesCollection.NewSeries
**ActiveChart.SeriesCollection(1).XValues = "=Tabelle1!Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(0, 7))"**
ActiveChart.SeriesCollection(1).Values = "=0; Tabelle1!ActiveCell.Offset(0, 11);0"
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
End Sub
提前致谢!
安东尼,
您不必将此视为将公式分配给单元格/XValues 属性 - 现在您实际上是在分配一个字符串并期望 Excel 将其计算为一个公式。相反,它应该简单地使用 ThisWorkBook.Worksheets("Tabelle1") 来分配以引用工作表 -
ActiveChart.SeriesCollection(1).XValues = ThisWorkBook.Worksheets("Tabelle1").Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(0, 7))
第二行需要传递一个数组/单元格范围尝试跟随-
ActiveChart.SeriesCollection(1).Values = Array(0, ThisWorkBook.Worksheets("Tabelle1").ActiveCell.Offset(0, 11),0)
此处有更多详细信息 -
https://msdn.microsoft.com/en-us/library/office/ff838988.aspx
我对 VBA 完全陌生。尝试将以下代码放在一起:存在具有多种风险的数据集。用户单击命令按钮,弹出一条消息 "please select a risk to be graphed",用户从数据库中选择一个风险(E 列中的一个)。之后一切都自动化了:插入散点图,选择三个 x 轴值(它们在与所选风险相同的行中的 J、K、L 列中),y 轴的三个值是 0 , 列 P 中的值与 rist 相同,0。绘制图形并设置格式后。格式化非常简单,到目前为止我还停留在第一部分。这是我到目前为止所拥有的。错误出现在行 selects x-values, "the element with this name was not found" 上。该行标记为 **
Sub Makro2()
Dim rng As Range
Dim InputRng As Range, OutRng As Range
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Choose the risk to draw", xTitleId, InputRng.Address, Type:=8)
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.SeriesCollection.NewSeries
**ActiveChart.SeriesCollection(1).XValues = "=Tabelle1!Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(0, 7))"**
ActiveChart.SeriesCollection(1).Values = "=0; Tabelle1!ActiveCell.Offset(0, 11);0"
ActiveSheet.ChartObjects("Diagramm 1").Activate
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
End Sub
提前致谢!
安东尼,
您不必将此视为将公式分配给单元格/XValues 属性 - 现在您实际上是在分配一个字符串并期望 Excel 将其计算为一个公式。相反,它应该简单地使用 ThisWorkBook.Worksheets("Tabelle1") 来分配以引用工作表 -
ActiveChart.SeriesCollection(1).XValues = ThisWorkBook.Worksheets("Tabelle1").Range(ActiveCell.Offset(0, 5), ActiveCell.Offset(0, 7))
第二行需要传递一个数组/单元格范围尝试跟随-
ActiveChart.SeriesCollection(1).Values = Array(0, ThisWorkBook.Worksheets("Tabelle1").ActiveCell.Offset(0, 11),0)
此处有更多详细信息 -
https://msdn.microsoft.com/en-us/library/office/ff838988.aspx