Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Member Polls
whats your favorite language

Sales Prediction Using Past Data in Excel (Simple VBA Project)

Last updated on 1 day ago
K
KevinSenior Member
Posted 1 day ago
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
K
KevinSenior Member
Posted 1 day ago
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
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 11
Members Online 0

Total Members: 21
Newest Member: brijamohanjha