Skip to content

Instantly share code, notes, and snippets.

@hyperupcall
Last active January 23, 2020 12:32
Show Gist options
  • Save hyperupcall/adc8d4ec8c0a1808a3866bf0c147bc70 to your computer and use it in GitHub Desktop.
Save hyperupcall/adc8d4ec8c0a1808a3866bf0c147bc70 to your computer and use it in GitHub Desktop.
excel macro for updating some charts
Sub recreateEdwinForecast(dateDifference As Integer, startRow As String, endRow As String, startDate As String, endDate As String)
Sheets("Edwin").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Main").Select
Range("A" & startRow & ":C" & endRow).Select
ActiveWorkbook.CreateForecastSheet Timeline:=Sheets("Main").Range("A" & startRow & ":A" & endRow), _
Values:=Sheets("Main").Range("C" & startRow & ":C" & endRow), ForecastEnd:=endDate, ConfInt _
:=0.95, Seasonality:=1, ChartType:=xlForecastChartTypeLine, Aggregation _
:=xlForecastAggregationAverage, DataCompletion:= _
xlForecastDataCompletionInterpolate, ShowStatsTable:=False
Sheets(ActiveSheet.Name).Name = "Edwin"
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.Shapes("Chart 1").IncrementLeft 216
ActiveSheet.Shapes("Chart 1").IncrementTop -93.4285826772
End Sub
Sub recreateLeonForecast(dateDifference As Integer, startRow As String, endRow As String, startDate As String, endDate As String)
Sheets("Leon").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Main").Select
Range("A" & startRow & ":B" & endRow).Select
ActiveWorkbook.CreateForecastSheet Timeline:=Sheets("Main").Range("A" & startRow & ":A" & endRow), _
Values:=Sheets("Main").Range("B" & startRow & ":B" & endRow), ForecastEnd:=endDate, ConfInt _
:=0.95, Seasonality:=1, ChartType:=xlForecastChartTypeLine, Aggregation _
:=xlForecastAggregationAverage, DataCompletion:= _
xlForecastDataCompletionInterpolate, ShowStatsTable:=False
Sheets(ActiveSheet.Name).Name = "Leon"
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.Shapes("Chart 1").IncrementLeft 216
ActiveSheet.Shapes("Chart 1").IncrementTop -93.4285826772
End Sub
Sub recreateCombinedForecast(forecastUntil As String)
Sheets("Combined").Select
ActiveWindow.SelectedSheets.Delete
Sheets.Add After:=ActiveSheet
Sheets(ActiveSheet.Name).Select
Sheets(ActiveSheet.Name).Name = "Combined"
Range("A1").Select
ActiveCell.FormulaR1C1 = "Timeline"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Edwin"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Leon"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=Edwin!RC"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & forecastUntil), Type:=xlFillDefault
Range("A2:A" & forecastUntil).Select
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(Edwin!RC[1],Edwin!RC[1],Edwin!RC)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & forecastUntil), Type:=xlFillDefault
Range("B2:B" & forecastUntil).Select
Range("C2").Select
ActiveCell.FormulaR1C1 = "=IF(Leon!RC,Leon!RC,Leon!RC[-1])"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & forecastUntil), Type:=xlFillDefault
Range("C2:C" & forecastUntil).Select
Range("A1:C" & forecastUntil).Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("Combined!$A$1:$C$" & forecastUntil)
ActiveChart.Location Where:=xlLocationAsObject, Name:="Main"
End Sub
Sub formatShapes(startRow As String, endRow As String)
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.Name = "Chart 1" Then
shp.IncrementLeft -20
shp.IncrementTop -165
shp.ScaleWidth 1.1334425893, msoFalse, _
msoScaleFromTopLeft
shp.ScaleHeight 1.1836388914, msoFalse, _
msoScaleFromTopLeft
End If
Next shp
End Sub
Sub updateForecast()
Dim startingDate As String
Range("C3").Select
startingDate = ActiveCell.FormulaR1C1
Dim endingDate As String
Range("D3").Select
endingDate = ActiveCell.FormulaR1C1
Dim dateDifference As Integer
dateDifference = ThisWorkbook.Sheets("Main").Cells(3, 5).Value
Dim startRow As String
Range("F3").Select
startRow = ActiveCell.FormulaR1C1
Dim endRow As String
Range("G3").Select
endRow = ActiveCell.FormulaR1C1
'Delete shapes that will be programmatically created later
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If (shp.Name = "Chart 1" Or shp.Name = "Chart 1030") Then
shp.Delete
End If
Next shp
Application.DisplayAlerts = False
Call recreateEdwinForecast(dateDifference, startRow, endRow, startingDate, endingDate)
Call recreateLeonForecast(dateDifference, startRow, endRow, startingDate, endingDate)
Call recreateCombinedForecast(dateDifference + 2)
Call formatShapes(startRow, endRow)
Application.DisplayAlerts = True
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment