[英]How do I arrange charts on an excel sheet using VBA?
I have a large sets of data arranged in an excel workbook. 我在Excel工作簿中安排了大量数据。 Each set of data contains R4,C192, and each sheet contains ten sets of data.
每组数据包含R4,C192,每张纸包含十组数据。 This code creates ten charts, one for each data set.
此代码创建十个图表,每个数据集一个。 After I create the charts, they are stacked on on top of another.
创建图表后,它们会堆叠在另一个图表之上。 I need to move them around so that they are arranged logically.
我需要移动它们,以便它们按逻辑排列。
This is a task I need to do thousands of times. 这是我需要做数千次的任务。 I worked a previous solution with unstable outcomes.
我以前的解决方案工作结果不稳定。
What I want What I have 我想要 什么
Sub CreateCharts()
'This is where my variable names are stored, for titles.
Sheets("names").Select
Trial = "motor_pre"
'loop interates through subject names (k loop)
For k = 2 To 19
subj = Worksheets("names").Cells(k, 1).Text
If subj = "end" Then End
x = 1
'innerloop iterates through regions (j loop)
For j = 2 To 11
' m = j - 1
Sheets("names").Activate
Reg = Worksheets("names").Cells(j, 3).Text
start_data = Worksheets("names").Cells(j, 8)
end_data = Worksheets("names").Cells(j, 9)
Sheets(subj).Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("'" & subj & "'!" & start_data _
& "$4:" & end_data & "$153")
ActiveChart.FullSeriesCollection(1).XValues = "='" & subj & _
"'!$H$4:$H$153"
ActiveChart.ChartTitle.Text = subj & " " & Reg
ActiveChart.Legend.Delete
Next j
Next k
End Sub
You can place the charts at the right place while proceeding. 您可以在继续操作时将图表放置在正确的位置。 But since your routine is working correctly, I won't touch it, just launch this macro afterwards to reorganize them.
但是由于您的例程正常工作,因此我不会碰它,只需稍后启动此宏即可对其进行重组。
Sub ReorganizeCharts()
Dim cht As ChartObject, left As Long, top As Long
' Modify these parameters to your linking
Dim chtWidth As Long, chtHeight As Long, chartsPerRow As Long
chtWidth = 200: chtHeight = 200: chartsPerRow = 4
Application.ScreenUpdating = False: Application.EnableEvents = False
On Error GoTo Cleanup
For Each cht In Sheets("names").ChartObjects
'adjust coordinates for next chart object
With cht
.top = top: .left = left: .Width = chtWidth: .Height = chtHeight
End With
left = left + chtWidth
If left > chartsPerRow * chtWidth * 0.99 Then
left = 0
top = top + chtHeight
End If
Next
Cleanup:
Application.ScreenUpdating = True: Application.EnableEvents = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.