我能到达这里的任何帮助都非常感激!我是VBA的新手,并且在这段编码中苦苦挣扎了一段时间,但距离还很远。
我正在尝试在excel中创建一个宏,该宏将为单独的数据范围创建多个散点图。
经过大量的学习,我提出了以下代码:
Sub CreateChart()
' Prepare coordinates for placing the graph in GW Charts Sheet
Dim y As Integer
y = 0
Dim x As Integer
x = 0
' Prepare chart series names
Dim name1 As String
Dim name2 As String
' Select the Gross Weight Worksheet
Sheets("GW").Select
' Select chart type to create
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
' Set chart dimensions
ActiveSheet.Shapes("Chart 7").IncrementLeft 1204.4118110236
ActiveSheet.Shapes("Chart 7").IncrementTop 190.5881889764
' Format Y Axis Units
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.NumberFormat = "#,##0"
' Format X Axis Units
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 1
ActiveChart.Axes(xlCategory).MaximumScale = 10
ActiveChart.Axes(xlCategory).MajorUnit = 1
ActiveChart.Axes(xlCategory).MinorUnit = 1
Selection.TickLabels.NumberFormat = "#,##0"
' Set X Axis Title
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Year & Quarter"
' Set Y Axis Title
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Gross Weight (Metric Tonnes)"
' Prepare for loop by creating variables
Dim w As Integer
w = 4
Dim z As Integer
z = 5
Dim counter As Integer
counter = 1
'This bit needs looping
Do Until counter = 90
' Set chart series names
name1 = (Range("B" & w).Value) & (" ") & ("In")
name2 = (Range("B" & z).Value) & (" ") & ("Out")
' Set name of series one (GW In)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = name1
ActiveChart.SeriesCollection(1).Values = (Range("E" & w, "N" & w))
' Set name of series two (GW Out)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = name2
ActiveChart.SeriesCollection(2).Values = (Range("E" & z, "N" & z))
' Set Chart Title
ActiveChart.HasTitle = True
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = (Range("C" & w))
' Move chart into GW Charts sheet
ActiveChart.Location Where:=xlLocationAsObject, Name:="GW Charts"
' Position chart on GW Charts sheet
With ActiveChart
.Parent.Top = y
.Parent.Left = x
End With
w = w + 2
z = z + 2
y = y + 200
counter = counter + 1
Loop
End Sub
我现在需要做的是找到一种使循环起作用的方法,以便它可以通过以下方式创建多个图形:
很抱歉,很长的问题,我花了很长时间才编写代码以生成1个正确的图形,现在我完全陷入了如何循环它的问题。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
成功!
我现在创建了下面的代码,效果很好!
Sub CreateChart()
' Select an empty cell to prevent error
Range("P1").Select
' Turn screen updating off so hopefully this whole thing doesn't crash and burn
Application.ScreenUpdating = False
' Prepare coordinates for placing the graph in GW Charts Sheet
Dim y As Integer
y = 0
Dim x As Integer
x = 0
' Prepare for loop by creating variables
Dim w As Integer
w = 4
Dim z As Integer
z = 5
Dim counter As Integer
counter = 1
' Enter the loop
Do Until counter = 82
' Prepare chart series names
Dim name1 As String
Dim name2 As String
' Select the Gross Weight Worksheet
Sheets("GW").Select
' Select chart type to create
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
' Set chart dimensions
ActiveSheet.Shapes("Chart 7").IncrementLeft 1204.4118110236
ActiveSheet.Shapes("Chart 7").IncrementTop 190.5881889764
' Format Y Axis Units
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.NumberFormat = "#,##0"
' Format X Axis Units
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 1
ActiveChart.Axes(xlCategory).MaximumScale = 10
ActiveChart.Axes(xlCategory).MajorUnit = 1
ActiveChart.Axes(xlCategory).MinorUnit = 1
Selection.TickLabels.NumberFormat = "#,##0"
' Set X Axis Title
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Year & Quarter"
' Set Y Axis Title
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Gross Weight (Metric Tonnes)"
' Set chart series names
name1 = (Range("B" & w).Value) & (" ") & ("In")
name2 = (Range("B" & z).Value) & (" ") & ("Out")
' Set name of series one (GW In)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = name1
ActiveChart.SeriesCollection(1).Values = (Range("E" & w, "N" & w))
' Set name of series two (GW Out)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = name2
ActiveChart.SeriesCollection(2).Values = (Range("E" & z, "N" & z))
' Set Chart Title
ActiveChart.HasTitle = True
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = (Range("C" & w))
' Move chart into GW Charts sheet
ActiveChart.Location Where:=xlLocationAsObject, Name:="GW Charts"
' Position chart on GW Charts sheet
With ActiveChart
.Parent.Top = y
.Parent.Left = x
End With
w = w + 2
z = z + 2
y = y + 250
counter = counter + 1
Loop
' Turn screen updating back on
Application.ScreenUpdating = True
' Display a friendly message box
MsgBox "Graphing Complete :)"
End Sub
我的问题是在代码中放置循环。希望这可以帮助遇到类似问题的任何人。
只需在W和Z变量上更改+2,即可一次捕获更多/更少的行:)