我有一个关于如何使用 vba 代码自动创建绘图(图表)的问题。
我可以有一个包含两种列的 Excel 文档:可分为 6 列或可分为 7 列。
前两张图片代表我如何接收 Excel 文档。
我要做的是:
Step 1.复制 A 列并将其放在每组 6 或 7 列之前,并插入一个空列,如图 3 所示。
Step 2.为新工作表中创建的每个新组创建一个图表(例如,如果我有 100 组列,我想要 100 张图。每个图都在一张表上)
The 问题是: 如何将每个图表放在单独的表格中?
如果需要,第一张纸的名称是"HOOD"
我编写的代码可以执行步骤 1,还可以创建绘图,但问题是我无法将每个图形都放在一张纸上。
我可以执行步骤 1,从步骤 2 开始,我只能创建图表,但无法将每个图表放入新工作表中。
Sub Macro_Linearity_Plot()
Dim pas As Integer
Dim val As Integer
Dim lCol As Integer
Dim i As Integer
Dim uCol As Integer
' define the numbers of columns. it can be 6 or 7 columns.
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
val = Range("A1").Value
pas = val + 2
' insert 2 new empty columns
For colx = pas To lCol Step pas
Columns(colx).Insert Shift:=xlToRight
Columns(colx).Insert Shift:=xlToRight
Next
' insert column number 1
For colx = pas + 1 To lCol Step pas
Sheets("HOOD").Columns(1).Copy
Sheets("HOOD").Columns(colx).PasteSpecial xlPasteValues
Next
' for every group of columns created at the last step generate a chart
uCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = -1 To uCol Step pas
Range(Cells(2, i + 2), Cells(121, i + pas)).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(Cells(2, i + 2), Cells(121, i + pas))
ActiveChart.ChartType = xl3DArea
Next
End Sub
谢谢 :)
UPDATED
新代码是:
Sub Macro_Linearity_Plot()
Dim pas As Integer
Dim val As Integer
Dim lCol As Integer
Dim i As Integer
Dim uCol As Integer
' define the numbers of columns. it can be 6 or 7 columns.
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
val = Range("A1").Value
pas = val + 2
' insert 2 new empty columns
For colx = pas To lCol Step pas
Columns(colx).Insert Shift:=xlToRight
Columns(colx).Insert Shift:=xlToRight
Next
' insert column number 1
For colx = pas + 1 To lCol Step pas
Sheets("HOOD").Columns(1).Copy
Sheets("HOOD").Columns(colx).PasteSpecial xlPasteValues
Next
' for every group of columns created at the last step generate a chart
uCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = -1 To uCol Step pas
Range(Cells(2, i + 2), Cells(121, i + pas)).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(Cells(2, i + 2), Cells(121, i + pas))
ActiveChart.ChartType = xl3DArea
xx = 1 'Just to identify the Graph order
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Chart" & xx
'Count the sheets and Charts for moving Chart to the end
ws = ThisWorkbook.Worksheets.Count
cht = ThisWorkbook.Charts.Count
Sheets("Chart" & xx).Move After:=Sheets(ws + cht)
xx = xx + 1
Next
End Sub
但也有一些错误: