ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Multiple For/Next Loop help (https://www.excelbanter.com/excel-programming/369392-multiple-next-loop-help.html)

[email protected]

Multiple For/Next Loop help
 
Hello. Can somebody suggest a revised version of the following code
please: -

For I = 6 To ilastcolumn Step 42
For j = 7 To ilastcolumn Step 42
For k = 9 To ilastcolumn Step 42
For l = 35 To ilastcolumn Step 42
For m = 37 To ilastcolumn Step 42
For n = 38 To ilastcolumn Step 42
For o = 40 To ilastcolumn Step 42
For p = 41 To ilastcolumn Step 42
For q = 42 To ilastcolumn Step 42

textstring = (Cells(ActiveCell.Row, I).Value) & " " &
(Cells(ActiveCell.Row, j).Value) _
& " " & (Cells(ActiveCell.Row, k).Value) & " " & (Cells(ActiveCell.Row,
l).Value) _
& " " & (Cells(ActiveCell.Row, m).Value) & " " & (Cells(ActiveCell.Row,
n).Value) _
& " " & (Cells(ActiveCell.Row, o).Value) & " " & (Cells(ActiveCell.Row,
p).Value) _
& " " & (Cells(ActiveCell.Row, q).Value) & Chr(10)


Next q
Next p
Next o
Next n
Next m
Next l
Next k
Next j
Next I

I need every variable from i-q incrememented by it's next value each
time the (textstring = .. ) statement is encountered. I tried to
achieve that with the above code but i think i've coded it wrong, i
think instead of that what is actually happening is each variable is
going through it's increment cycle independently of the other over
variables before moving on to the next variable, which is running it's
own incremement cycle.

Any ideas how I get all the variables to incremement to there next
value at the same time?

thanks,

Gary.


JonR

Multiple For/Next Loop help
 
Try this (it's sort of cumbersome, but should work)

j = 7
k = 9
l = 35
m = 37
n = 38
o = 40
p = 41
q = 42

For I = 6 To ilastcolumn Step 42

textstring = (Cells(ActiveCell.Row, I).Value) & " " &
(Cells(ActiveCell.Row, j).Value) _
& " " & (Cells(ActiveCell.Row, k).Value) & " " & (Cells(ActiveCell.Row,
l).Value) _
& " " & (Cells(ActiveCell.Row, m).Value) & " " & (Cells(ActiveCell.Row,
n).Value) _
& " " & (Cells(ActiveCell.Row, o).Value) & " " & (Cells(ActiveCell.Row,
p).Value) _
& " " & (Cells(ActiveCell.Row, q).Value) & Chr(10)

if j+42 = ilastcolumn then
j= ilastcolumn
else
j=J+42
end if

if k+42 = ilastcolumn then
k= ilastcolumn
else
k=k+42
end if

if l+42 = ilastcolumn then
l= ilastcolumn
else
l=l+42
end if

if m+42 = ilastcolumn then
m= ilastcolumn
else
m=m+42
end if

if n+42 = ilastcolumn then
n= ilastcolumn
else
n=n+42
end if

if o+42 = ilastcolumn then
o= ilastcolumn
else
o=o+42
end if

if p+42 = ilastcolumn then
p= ilastcolumn
else
p=p+42
end if

if q+42 = ilastcolumn then
q= ilastcolumn
else
q=q+42
end if

Next I


--
HTH

JonR


" wrote:

Hello. Can somebody suggest a revised version of the following code
please: -

For I = 6 To ilastcolumn Step 42
For j = 7 To ilastcolumn Step 42
For k = 9 To ilastcolumn Step 42
For l = 35 To ilastcolumn Step 42
For m = 37 To ilastcolumn Step 42
For n = 38 To ilastcolumn Step 42
For o = 40 To ilastcolumn Step 42
For p = 41 To ilastcolumn Step 42
For q = 42 To ilastcolumn Step 42

textstring = (Cells(ActiveCell.Row, I).Value) & " " &
(Cells(ActiveCell.Row, j).Value) _
& " " & (Cells(ActiveCell.Row, k).Value) & " " & (Cells(ActiveCell.Row,
l).Value) _
& " " & (Cells(ActiveCell.Row, m).Value) & " " & (Cells(ActiveCell.Row,
n).Value) _
& " " & (Cells(ActiveCell.Row, o).Value) & " " & (Cells(ActiveCell.Row,
p).Value) _
& " " & (Cells(ActiveCell.Row, q).Value) & Chr(10)


Next q
Next p
Next o
Next n
Next m
Next l
Next k
Next j
Next I

I need every variable from i-q incrememented by it's next value each
time the (textstring = .. ) statement is encountered. I tried to
achieve that with the above code but i think i've coded it wrong, i
think instead of that what is actually happening is each variable is
going through it's increment cycle independently of the other over
variables before moving on to the next variable, which is running it's
own incremement cycle.

Any ideas how I get all the variables to incremement to there next
value at the same time?

thanks,

Gary.



[email protected]

Multiple For/Next Loop help
 
You are a genius, a gentleman and a scholar.

You do not know how much trouble you have saved me.

I can not express my gratitude!

Thanks.



All times are GMT +1. The time now is 04:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com