Revised code
Sub x()
Dim nMinAP As Long, nMaxAP As Long, nStepAP As Long
Dim nMinAG As Long, nMaxAG As Long, nStepAG As Long
Dim i As Long, nLastcol As Long, nlastrow As Long
Dim rRow, rCol, rRef, j As Long
Dim rStart As Range, rData As Range
Dim lngCalcMode As XlCalculation
Dim rngChartOutput As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
lngCalcMode = .Calculation
.Calculation = xlCalculationManual
End With
On Error Resume Next
Sheets("Charts").Delete
On Error GoTo 0
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Charts"
Set rngChartOutput = Worksheets("Charts").Range("B2:H14")
Sheets("Mov_Avg_Chart").Activate
nMinAP = Range("B22").Value
nMaxAP = Range("B23").Value
nStepAP = Range("B24").Value
nMinAG = Range("B25").Value
nMaxAG = Range("B26").Value
nStepAG = Range("B27").Value
Set rRow = Range("C8")
Set rCol = Range("C6")
rRef = Array("AF7", "AF9", "AF10", "AF11")
nlastrow = Cells.Find(What:="*", After:=Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
nLastcol = Cells.Find(What:="*", After:=Cells(1, 1), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
If nLastcol < 9 Then
Range(Cells(21, "AE"), Cells(nlastrow, "AE")).Clear
Else
Range(Cells(21, "AE"), Cells(nlastrow, nLastcol)).Clear
End If
Set rStart = Range("AE21")
For i = LBound(rRef) To UBound(rRef)
With rStart
.Offset(1).Value = nMinAP
.Offset(1).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=nStepAP, Stop:=nMaxAP
.Offset(, 1).Value = nMinAG
.Offset(, 1).DataSeries Rowcol:=xlRows, Type:=xlLinear, Step:=nStepAG, Stop:=nMaxAG
With .CurrentRegion
.Rows(1).Font.Bold = True
.Columns(1).Font.Bold = True
End With
End With
Set rData = rStart.Offset(1, 1).CurrentRegion
With rData
.NumberFormat = "0.00"
.Table RowInput:=rRow, ColumnInput:=rCol
.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlSurfaceTopView
ActiveChart.Location xlLocationAsObject, "Charts"
With ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
.Left = rngChartOutput.Left
.Top = rngChartOutput.Top
.Width = rngChartOutput.Width
.Height = rngChartOutput.Height
End With
If i Mod 2 = 0 Then
' move to right
Set rngChartOutput = rngChartOutput.Offset(, rngChartOutput.Columns.Count + 2)
Else
' move down and left
Set rngChartOutput = rngChartOutput.Offset(rngChartOutput.Rows.Count + 2, -rngChartOutput.Columns.Count - 2)
End If
Sheets("Mov_Avg_Chart").Activate
End With
With rStart
.Formula = "=" & rRef(i)
.Interior.ColorIndex = 17
End With
Set rStart = rStart.Offset(rStart.CurrentRegion.Rows.Count + 1)
Next i
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
.Calculate
.Calculation = lngCalcMode
End With
End Sub
Bookmarks