VBA

crkennedy

New Member
Joined
Dec 15, 2016
Messages
4
I'm trying to create a function for implementing simple exponential smoothing in this VBA spreadsheet.
I've gotten code I've amended from online to suit my needs but it's running incorrectly, it only pastes 0's in column C.
Could anyone advise?

VBA Code:
Sub ExpSmoothing()
Dim y As Double
Dim Lastyhat As Double
Dim Currentyhat As Double
Dim i As Integer
Dim alpha As Variant

alpha = Application.InputBox("What is your alpha value?")

If alpha > 0 And alpha < 1 And IsNumeric(alpha) Then
With Worksheets("Data")
.Range("C1").Value = "Exp Smoothing"

For i = 1 To 15

.Range(.Cells(i + 1, 3), .Cells(14, 3)).Value = y

.Range("C2" & i).Value = Lastyhat

Currentyhat = Lastyhat + (alpha * (y - Lastyhat))

Lastyhat = Currentyhat

.Range("C" & i + 1).Value = Currentyhat


Next i
End With
Else
MsgBox ("Please choose an alpha that is numeric and between 0 and 1")
End If
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hello Crkennedy,
first...
When you insert value into input box, code will treat value as text.
You need to find way to convert it into decimal number.
I don't know what you are trying to do but first you can try to do something like this.
VBA Code:
    Dim alpha As Variant
    
    alpha = Application.InputBox("What is your alpha value?")
    If InStr(1, alpha, ".") Then
        alpha = CDbl(alpha) / 10 ^ (Len(alpha) - 2)
    Else
        MsgBox "This not right format! The right format is 0.00"
        Exit Sub
    End If

Anything else?
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top