excel vba - Why array Index starts at 1 when passing range values to array -
in vba program trying pass array spreadsheet , add 1 each of array's cells. problem index of array. when start looping array doesnt work when start index 0 ( error subscript out of range) works when start array 1. why that? (i thought case specify @ top option base 1)
sub passarray() dim array variant dim i, j integer 'pass array , manipulate vol = range("volatility") = 0 2 j = 0 2 vol(i, j) = 1+ vol(i,j) next j next end sub
that wasn't case when pass range arrays based on experience.
don't know specific reason behind, this link indicates cannot change behavior.
quote: array worksheet data loaded has lower bound (lbound) equal 1, regardless of option base directive may have in module. cannot change behavior.
what can utilize use of lbound/ubound this:
vol = range("volatility") = lbound(vol, 1) ubound(vol, 1) j = lbound(vol, 2) ubound(vol, 2) '~~> stuff here vol(i, j) = 1 + vol(i, j) next j next
if range 1 column several rows, pass array this:
vol = application.transpose(range("volatility")) = lbound(vol) ubound(vol) '~~> stuff here vol(i) = 1 + vol(i) next
this way, produce one-d array instead of two-d array.
iterate values can use above or can use for each:
dim x variant '~~> dimension variant variable each x in vol '~~> stuff here x = 1 + x next
Comments
Post a Comment