vba - Creating an array variable with a variable number of elements -


i want define array variable have variable number of elements depending on m number of results returned search. error "constant expression required" on:

dim cmodels(0 m) string 

here complete code

dim foundrange range dim rangetosearch range set rangetosearch = selection set foundrange = rangetosearch.find(what:="lights", after:=activecell,             lookin:=xlvalues, lookat:= _ xlpart, searchorder:=xlbyrows, searchdirection:=xlnext, matchcase:=false _ , searchformat:=false) 'first occurrence     m = worksheetfunction.countif(selection, "lights")  dim secondaddress string if (not foundrange nothing)     dim count integer: count = 0     dim targetoccurrence integer: targetoccurrence = 2     dim found boolean     z = 1     dim cmodels(0 m) string     until z = m     z = z + 1         foundrange.activate         set foundrange = rangetosearch.findnext(foundrange)         if not foundrange.next nothing         z(m) = activecell(offset(0, 2))         end if     loop     end if end sub 

see following code comments:

sub redimvsredimpreserve()      'i declare arrays know resize     'without fixed size initially..     dim cmodels() string     dim m integer     m = 3      'this wipe out data in array     redim cmodels(0 m) string      'you can use if want save information in variable     cmodels(2) = "test"     redim preserve cmodels(0 m) string      'this still keep "test"     debug.print "with redim preserve, value is: " & cmodels(2)      'using 'redim'     redim cmodels(0 m) string     debug.print "with redim, value is: " & cmodels(2)    end sub 

also note using redim preserve (such loop) can operation takes time.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -