Código VBA para Excel. Como criar gráficos em folhas separadas?
Eu tenho uma pergunta sobre como criar gráficos (gráfico) automaticamente usando o código vba. Posso ter um documento do Excel com dois tipos de colunas: colunas que podem ser agrupadas em 6 ou colunas que podem ser agrupadas em 7. As duas primeiras figuras representam como recebo o documento do Excel.
O que tenho que fazer é:
Passo 1. copiar a coluna A e colocá-la antes de cada grupo de 6 ou 7 colunas e inserir também uma coluna vazia, como na figura 3.
Passo 2. para criar um gráfico para cada novo grupo criado em uma nova planilha (por exemplo, se eu tiver 100 grupos de colunas, desejo ter 100 planilhas de plotagem. cada plotagem em uma única planilha)
oquestão é: Como colocar todos os gráficos em folhas separadas?
Se necessário, o nome da primeira folha é"CAPUZ"
O código escrito por mim pode executar a etapa 1 e também cria gráficos, mas o problema é que não consigo colocar todos os gráficos em uma única planilha.
Eu posso executar a Etapa 1 e a partir da Etapa 2 só posso criar os gráficos, mas não consigo colocar todos os gráficos em uma nova planilha.
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
Obrigado :)
ATUALIZADA
O novo código é:
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
Mas existem alguns erros: