excel - Subscript out of range when trying to access 2d array value using variables -
i have workbook needs open workbook, copy range , paste in first workbook. range being copied 2d array. before pasting value, need check if value blank doesn't paste 0 table. after should paste value original spreadsheet , loop next value. array needs dynamic function run different sized tables. number of columns change, number of rows constant.
the getvalue2
function passes range myarray
fine "subscript out of range" error occurs when try access individual values in myarray
using variables ro
, col
.any insight how fix this? see code below.
public function multicolgetvalsrev2(startj integer, starti integer, endj integer, endi integer, startc integer, startr integer, endc integer, endr integer, instcountrow integer, instcountcol integer, p string, f string, s string, thisworksheet string) dim myarray() variant dim colnum integer dim rownum integer dim ro integer dim col integer dim path, file, sheet string dim wkbk workbook path = p file = f sheet = s colnum = endc - startc rownum = 24 ro = 0 col = 0 redim myarray(rownum, colnum) 'getvalue2 call myarray = getvalue2(path, file, startr, endr, startc, endc) redim preserve myarray(rownum, colnum) 'paste values flow map j = startj endj = starti endi if col <= colnum if ro <= rownum 'clear existing cell thisworkbook.sheets(thisworksheet).cells(i, j).clear 'check if cell blank if myarray(ro, col) = isblank 'error occurs here myarray(ro, col) = "" end if 'paste value flow map thisworkbook.sheets(thisworksheet).cells(i, j).value = myarray(ro, col) end if ro = ro + 1 end if next col = col + 1 ro = 0 next j end function
the getvalue2
function follows:
private function getvalue2(path, file, startr, endr, startc, endc) dim wb workbook application.screenupdating = false application.asktoupdatelinks = false set wb = workbooks.open(path & application.pathseparator & file) wb.sheets(1).activate getvalue2 = range(cells(startr, startc), cells(endr, endc)) wb.close (false) application.asktoupdatelinks = true application.screenupdating = true end function
Comments
Post a Comment