I want to do repeated loops without writing “for X=…” many times. Is there a way to achieve this?
As a theoretical example, say I have an array R or 2 elements, (0, 1). I want to generate all the combinations of 0 and 1 taken together say 5 times, using this set array. So that would mean 5 loops. So I could write:
Dim outputString as string
For T= lbound(R) to ubound(R)
For W=lbound(R) to ubound(R)
For Y=lbound(R) to ubound(R)
For D=lbound(R) to ubound(R)
For Z=lbound(R) to ubound(R)
outputString=R(T) & R(W) & R(Y) & R(D) & R(Z)
Next Z
Next D
Next Y
next W
next T
Obviously this will do the job, bit it’s ugly and what if I want to do 30 loops?…I can’t imagine writing “For…Next” 30 times. Is there any way to repeat a for loop a variable number of times without coding them in individually? What if there were 20 different arrays and we’d need to combine them with loops? In that case coding individual loops is the only way to do it?
I often thought about this and tried with do/while loops, but did not end anywhere.
Rising Sun_7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.