In this sub, I'm simply doing a few calculations. In a differnt sheet I've some temporary data stored and I'm trying and failing to define the following as a range (at the very bottom at the code):
MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
I get the following error: 1004 application-defined or object-defined error
The thing though is, this is a line of code that I copied in from a different module, so I know the method itself works.
I assume the problem is with the activation of the sheets, I'm tried to activate both at the beginning but with no luck. I do specify the worksheet in evry line of code, so I'm not sure why it would still be a problem?
I've tried to select the range as well:
Set MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
All code:
Sub CalculateOwnPortfolio()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.StatusBar = "Calculating..."
Dim MeanVector As Range
Dim WeightsVector As Range
Worksheets("TempSheet").Activate
Worksheets("Own Portfolio").Activate
'Count the amount of stock
Stocks = 0
For i = 1 To 20
If Worksheets("MainSheet").Cells(i + 2, 2) <> 0 Then
Stocks = Stocks + 1
Else
Stocks = Stocks
End If
Next
'Amount of each stock
EmptyAmount = 0
For i = 1 To Stocks
If Worksheets("Own Portfolio").Cells(1 + i, 7) = Value Then
Else
EmptyAmount = EmptyAmount + 1
End If
Next
If EmptyAmount = 0 Then
MsgBox ("Error: Enter stock amounts")
Exit Sub
End If
'Calcualte amount of observations
Observations = 0
For j = 2 To 15000
If Worksheets("Own Portfolio").Cells(j, 1) <> 0 Then
Observations = Observations + 1
Else
Observations = Observations
End If
Next
Worksheets("Own Portfolio").Range(Cells(2, 2), Cells(Observations, 2)) _
.ClearContents
Worksheets("Own Portfolio").Range(Cells(2, 5), Cells(3 + Stocks, 5)) _
.ClearContents
'Total Value
For i = 2 To Observations + 1
Value = 0
For j = 1 To Stocks
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(i, 7)
Value = Value + (Amount * AdjClose)
Worksheets("Own Portfolio").Cells(i, 2) = Value
Next
Next
'Weights
TotalValue = 0
For j = 1 To Stocks
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(2, 7)
TotalValue = TotalValue + (Amount * AdjClose)
Next
For j = 1 To Stocks
StockValue = 0
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(2, 7)
StockValue = Amount * AdjClose
Worksheets("Own Portfolio").Cells(1 + j, 5) = StockValue / TotalValue
Next
'Mean,Variance,Std Dev and Sharp Ratio
'-------------------------------------------------------------------------
'----------------This is where I get the error message--------------------
MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
'-----------------------------------------------------------------------
WeightsVector = Worksheets("Own Portfolio").Range(Cells(2, 5), Cells(Stocks + 1, 2))
Mean = Application.WorksheetFunction.SumProduct(MeanVector, WeightsVector)
Call OwnPortfolioGraph(Symbol)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.StatusBar = False
End Sub
Picture of "TempSheet"