arrays - VBA Looping through selected columns with Cells() -


let's assume have spreadsheet 15 columns ans 20 rows. make array out of 2 columns (lets of 2nd , 10th) , rows. how it?

i suppose might starting point, dont know how define selected columns using cells() funcion.

dim array1(20, 2) single set arrays = sheets("sheet1")  = 1 20 j = 1 2 my_array(i - 1, j - 1) = arrays.cells(i, j - ??) 

if understood want generate 2 dimensional array containing data 2 of sheet columns. such thing custom function looking this:

' ----------------------------------------------------------------- ' getmydata - picks data sheet's first 20 rows , 2 columns '    - sheetname {string} - name of sheet data taken '    - col1 {integer} - first column read '    - col2 {integer} - second column read ' returns: 2-dimensional array containing data of columns parameters function getmydata(sheetname string, col1 integer, col2 integer) variant     ' declarations     dim sheet worksheet: set sheet = activeworkbook.sheets(sheetname)     dim result(20, 2) variant      ' logic     dim row integer, col integer     row = 1 20         result(row - 1, 0) = sheet.cells(row, col1)         result(row - 1, 1) = sheet.cells(row, col2)     next      ' return result     getmydata = result end function 

and can use that:

private sub commandbutton1_click()     ' data sheet3 first 20 rows columns 2(b) , 10(j)     data = getmydata("sheet3", 2, 10) end sub 

now couple of words function - since know want data 2 of columns can in single loop , job @ once here:

for row = 1 20     result(row - 1, 0) = sheet.cells(row, col1) ' take data col1     result(row - 1, 1) = sheet.cells(row, col2) ' take data col2 next 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -