我正在编写一个用于在Excel中绘制300个图表的脚本。在“工作表”中,数据按名称排序。名称列表显示在“主表”中。 VBA脚本对我来说是新的,但我从互联网上的不同问题得到了这个脚本。我想在图表范围和选择语句之间建立联系。你可以帮帮我吗?
Sub Startup()
Sheets("Worksheet").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
' Decide if to select based on column A
ThisValue = Sheets("worksheet").Cells(x, 1).Value
' SinglePath Configuration
If ThisValue = Sheets("Master Sheet").Range("B" & 2) Then
Sheets("worksheet").Range("A" & x, "C" & x).Select
End If
'Loop te create the SetSourceData selection for graph
Next x
Worksheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
ActiveChart.SetSourceData Source:=Range(Seclection) 'is selection a correct statement?
ActiveChart.PlotArea.Select
ActiveChart.Location Where:=xlLocationAsNewSheet
ws.Select
End Sub
答案 0 :(得分:0)
您的代码无法正常工作......例如:
Selection
ws
)更新了代码
以下代码应与您需要的代码一致
如果符合条件,则将一个范围(1rng1)输入到图表制作子图表中,此子图像会自动将每个图表放置在数据所在的工作表中的下拉位置(使用lngCnt
变量)。 / p>
Sub Startup()
Dim ws As Worksheet
Dim lngRow As Long
Dim lngCnt As Long
Application.ScreenUpdating = False
Set ws = Sheets("Worksheet")
' Find the last row of data
lngRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For lngCnt = 2 To lngRow
' Decide if to select based on column A
ThisValue = Sheets("worksheet").Cells(lngCnt, 1).Value
' SinglePath Configuration
If ThisValue = Sheets("Master Sheet").Range("B" & 2) Then Call MakeChart(Range("A" & lngCnt, "C" & lngCnt), lngCnt)
'Loop te create the SetSourceData selection for graph
Next lngCnt
Application.ScreenUpdating = True
End Sub
Sub MakeChart(rng1 As Range, ByVal lngCnt)
Dim ch As ChartObject
Set ch = ActiveSheet.ChartObjects.Add(200, (lngCnt - 1) * 100, 250, 150)
With ch
.Chart.ChartType = xlXYScatterLinesNoMarkers
.Chart.SetSourceData rng1
End With
End Sub