Run-time error '13': Type mismatch

rcg78

New Member
Joined
Dec 12, 2014
Messages
6
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!
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I only created 2 four column Ranges in each worksheet-- No VBA (Macro) needed...
Just enter formula as I did in cell B20, Format it 0.00% and copy to C20:E20; then select B20:E20 and copy down to B21:E35; Does that help?



Excel 2012
ABCDE
1
2289173162305
3155252147169
4377370383336
5427392371316
6238468294225
7289303106118
8340137240171
9223193296258
10388370328462
11234345311154
12380314169435
13415188229354
14397136237137
15367429289292
16185141360295
17464295111110
18
19
20-62.28%
Adjusted Close Price
Cell Formulas
RangeFormula
B20=(B2-Calculations!B2)/B2



Excel 2012
ABCDE
1
2469326316493
3390424258191
4350296452247
5101383479471
6251469467115
7138459196332
8239100225494
9251230323491
10367343203220
11240173162225
12417451409111
13229295194469
14227332438105
15255219421128
16197454209294
17427439181260
Calculations
 
Upvote 0
Dave,

Thanks for your response. I tried your suggestion but it didn't seem to change anything. I'm wondering if Excel is not interpreting the cells on "Adjusted Close Price" as numbers. Is there a way to tell what type of variable Excel is reading it as?
 
Upvote 0
Jim,

Thanks for your response and example. I will be updating this sheet on a regular basis, so I would prefer to automate this process with VBA. I appreciate your work though!
 
Upvote 0
maybe start from the beginning... where's the data, what do you want to do with it, and where is the outcome supposed to go. You have set stockprice as a range so it has no value.... each item in the range has a value. Dave
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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