Excel图表:使用用户在序列集合中的输入

Excel图表:使用用户在序列集合中的输入,excel,vba,Excel,Vba,我正在做一个程序,用户将输入他们想要的工作表的名称。之后,程序将使用用户指定的名称创建新的工作表和图表。代码如下所示: Option Explicit Dim NewWB As Workbook Dim thisWB As Workbook Dim i As Integer Dim test_num As Variant 'Dim sheetName As Variant Sub NewWSName() test_num = InputBox("Enter the test number:

我正在做一个程序,用户将输入他们想要的工作表的名称。之后,程序将使用用户指定的名称创建新的工作表和图表。代码如下所示:

Option Explicit

Dim NewWB As Workbook
Dim thisWB As Workbook
Dim i As Integer
Dim test_num As Variant
'Dim sheetName As Variant

Sub NewWSName()

test_num = InputBox("Enter the test number: (Ex: T1 for Test #1)", "Test Number")
If test_num <> "" Then
   Sheets.Add.Name = test_num & " Data"
End If

End Sub

Sub NewChart()

If test_num <> "" Then
   Charts.Add.Name = test_num & " Basic Chart"
End If
End Sub

Sub basic_10()
'Add chart sheet and clear the content
NewChart
ActiveChart.ChartArea.Clear

'Set the chart type as 3D Column
ActiveChart.ChartType = xl3DColumn

Set thisWB = ThisWorkbook
Set NewWB = ActiveWorkbook

With NewWB
    thisWB.Sheets("Basic Chart").ChartArea.Copy
    NewWB.Sheets(ActiveSheet.Name).Paste

    'NewWB.Charts(test_num & " Basic Chart").Activate
    'ActiveChart.ChartArea.Select
    'Here is the error start
    ActiveChart.SeriesCollection(1).Name "=' & test_num & ' Data'!$Q$12"
    ActiveChart.SeriesCollection(1).Values = "=' & test_num & ' Data'!$Q$13:$Q$44"
    ActiveChart.SeriesCollection(2).Name = "=' & test_num & ' Data'!$R$12"
    ActiveChart.SeriesCollection(2).Values = "='" & test_num & " Data!$R$13:$R$44"

End With
End Sub

在ActiveChart.SeriesCollection1.Name='和test_num&'Data'上$Q$12是我出错的地方。我如何使用用户的输入来分配我希望收集数据的位置?我已经尝试过其他方法,但效果不太好。我无法将其指定为Sheets1或类似的内容,因为我无法预测用户希望收集数据的次数。有更好的方法吗?谢谢您的帮助。

尝试将此代码添加到您的子项中以代替工作表。Add.Name=test\u num&Data:


您还需要对Charts.Add.Name执行类似的操作。

谢谢您的帮助,但我现在就知道了。我只需要更改ActiveChart.SeriesCollection1.Name='&test\u num&'Data'所在的部分$Q$12到ActiveChart.SeriesCollection1.Name=工作表1.范围$Q$12
Dim w As Worksheet

Set w = Sheets.Add

w.Name = test_num & " Data"