I'm very new to VBA and am trying to create a sub that pulls stock prices from one sheet in my workbook and calculates the percentage change between the prices in another sheet. The code isn't fully built out yet. I am trying to build step by step and have run into a problem in my variables/formula for calculating percentage change. I get the error message "Run-time error '13': Type mismatch" Here is the code I have so far (with the problem being noted with * *):
Private Sub CommandButton2_Click()
'Do Calculations
Dim percentage As Double, calcRange As range, stockPrice As range, n As Currency, o As Currency
Set calcRange = Worksheets("Calculations").range("B2:W17")
Set stockPrice = Worksheets("Adjusted Close Price").range("B2:W17")
With Worksheets("Adjusted Close Price")
*o = stockPrice.Value*
n = stockPrice.Offset(, 1).Value
calcRange.Value = (n - o)/o
End With
calcRange.NumberFormat = "0.00%"
End Sub
Any help is appreciated!
Private Sub CommandButton2_Click()
'Do Calculations
Dim percentage As Double, calcRange As range, stockPrice As range, n As Currency, o As Currency
Set calcRange = Worksheets("Calculations").range("B2:W17")
Set stockPrice = Worksheets("Adjusted Close Price").range("B2:W17")
With Worksheets("Adjusted Close Price")
*o = stockPrice.Value*
n = stockPrice.Offset(, 1).Value
calcRange.Value = (n - o)/o
End With
calcRange.NumberFormat = "0.00%"
End Sub
Any help is appreciated!
Last edited: