here is the vba code - without chart version
Sub PredictSales()
Dim lastRow As Integer
Dim avgSales As Double
Dim trend As String
' Find last data row
lastRow = Cells(Rows.Count, "B").End(xlUp).Row
' Calculate Moving Average (Last 3 Months)
avgSales = Application.WorksheetFunction.Average( _
Range("B" & lastRow - 2 & ":B" & lastRow))
' Display prediction
Range("E4").Value = Round(avgSales, 0)
Range("D3").Value = "Next Month"
' Trend logic
If Cells(lastRow, "B").Value > Cells(lastRow - 1, "B").Value Then
trend = "Upward Trend"
Else
trend = "Downward Trend"
End If
Range("D4").Value = "Trend"
Range("E4").Value = trend
MsgBox "Sales Prediction Completed!", vbInformation
End Sub
and with chart version : Sub PredictSales()
Dim ws As Worksheet
Dim lastRow As Long
Dim avgSales As Double
Dim trend As String
Dim chartObj As ChartObject
Dim chartLeft As Double, chartTop As Double, chartWidth As Double
Dim monthsArr() As Variant, salesArr() As Variant
Dim i As Long
Set ws = ActiveSheet
' Find last row
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
If lastRow < 4 Then
MsgBox "At least 3 months of sales data required", vbExclamation
Exit Sub
End If
' Calculate Moving Average (Last 3 Months)
avgSales = Application.WorksheetFunction.Average( _
ws.Range("B" & lastRow - 2 & ":B" & lastRow))
' ===== TEXT OUTPUT (USING YOUR REFERENCE) =====
ws.Range("D3").Value = "Next Month"
ws.Range("E3").Value = Round(avgSales, 0)
If ws.Cells(lastRow, "B").Value > ws.Cells(lastRow - 1, "B").Value Then
trend = "Upward Trend"
Else
trend = "Downward Trend"
End If
ws.Range("D4").Value = "Trend"
ws.Range("E4").Value = trend
' ==============================================
' Build arrays including prediction
ReDim monthsArr(1 To lastRow)
ReDim salesArr(1 To lastRow)
For i = 2 To lastRow
monthsArr(i - 1) = ws.Cells(i, "A").Value
salesArr(i - 1) = ws.Cells(i, "B").Value
Next i
monthsArr(lastRow) = "Next Month"
salesArr(lastRow) = ws.Range("E3").Value
' Remove old chart
For Each chartObj In ws.ChartObjects
chartObj.Delete
Next chartObj
' Chart position: C8 to I8
chartLeft = ws.Range("C8").Left
chartTop = ws.Range("C8").Top
chartWidth = ws.Range("I8").Left + ws.Range("I8").Width - chartLeft
' Create chart
Set chartObj = ws.ChartObjects.Add( _
Left:=chartLeft, _
Top:=chartTop, _
Width:=chartWidth, _
Height:=260)
With chartObj.Chart
.ChartType = xlLineMarkers
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "Sales + Prediction"
.SeriesCollection(1).XValues = monthsArr
.SeriesCollection(1).Values = salesArr
.HasTitle = True
.ChartTitle.Text = "Sales Trend with Prediction"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Month"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Sales"
' ?? ORANGE prediction point (LAST POINT)
With .SeriesCollection(1).Points(UBound(salesArr))
.MarkerSize = 10
.Format.Fill.ForeColor.RGB = RGB(255, 165, 0) ' Orange
.Format.Line.ForeColor.RGB = RGB(255, 165, 0)
End With
End With
MsgBox "Prediction and graph created successfully!", vbInformation
End Sub