excel - Subroutine returning all 0s -
i trying return values user defined function, returned 0s. feel values i'm assigning variables wk1 , wk2 aren't being used in function.
the goal of subroutine calculate weekly returns of stocks, given prices provided in worksheet "prices".
i'm not savvy vba appreciated
thanks help!
public sub wklyrtn() dim wk1, wk2 long dim row long, column long dim matrix1(2 261, 2 11) integer sheets("prices").select selection.activate row = 2 261 column = 2 11 wk2 = cells(row, column).value wk1 = cells(row + 1, column).value matrix1(row, column) = rtrn(wk1, wk2) next column next row sheets("returns").select selection.activate row = 2 261 column = 2 11 cells(row, column) = matrix1(row, column) next column next row end sub public function rtrn(wk1, wk2) dim delt long application.volatile true delt = wk2 - wk1 rtrn = delt / wk1 end function
try this. not sure trying matrix
. give values need. need refer object (your worksheets) instead of using select
(always avoid in general , try refer object instead using set
.
option explicit public sub wklyrtn() dim wk1 long, wk2 long dim row long, column long dim matrix1(2 261, 2 11) variant dim wks worksheet, wks2 worksheet set wks = thisworkbook.sheets("prices") wks row = 2 261 column = 2 11 wk2 = wks.cells(row, column).value wk1 = wks.cells(row + 1, column).value matrix1(row, column) = rtrn(wk1, wk2) next column next row end set wks2 = thisworkbook.sheets("returns") wks2 row = 2 261 column = 2 11 wks2.cells(row, column) = matrix1(row, column) next column next row end end sub
Comments
Post a Comment