Writing multiple for loops together

tarannam

New Member
Joined
Jul 31, 2019
Messages
5
Hi, I am writing a program in excel VBA where I want to write two for loops together. For example,
For x = 1 to 10 and For y = 1 to 10
do something ...
Next

But if I write "and" between two for statements the program shows error. If I put : between the for statements or write them separately in different lines then the program works in a different way which I don't want. What I want is for x =1, y should also be 1, for x =2, y should be 2 and so on. How can I do that? Please help. Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi & welcome to MrExcel.
I don't understand what you are asking.
If you need y to equal x then can't you just use one loop?
 
Upvote 0
Thanks.
Alright, I am asking my question again. I want to write a program like
For x = 1 to 10 and for y = 1 to 10
a = x*5 + y *3
Next
But if I write and between two for statements the program throws error.
Now if I write something like
For x = 1 to 10
For y = 1 to 10
 
Upvote 0
Sorry the question got posted halfway.

Ok, I am continuing the question ...

If I write two for statements separately then what the program does is for x = 1 it will go from 1 to 10 for y and again it will do the same for x =2 which I don't want. I hope it is clear now.
 
Upvote 0
If you want x and y to be the same why not just use
Code:
For x = 1 To 10
   A = x * 5 + x * 5
Next x
 
Upvote 0
No the original problem is a bit different. I was asking the question in a simplified form. Actually I was using array where array index for two variables are same but the values itself are not the same.
....
For x = LBound(a) To UBound(a) and For y = LBound(b) To UBound(b)
c(z) = Exp(b(y)/5)*(Exp(b(y) + a(y))
Next

Here I want when index of a is 1, b should also be 1. The full program is a long one, I am showing here the part where I have problem.
 
Upvote 0
As long as the arrays are the same size & the Lbound is the same for both just use
Code:
For x = LBound(A) To UBound(A)
c(z) = Exp(b(x)/5)*(Exp(b(x) + a(x))
Next
 
Upvote 0
Got it. Thanks. But just to know isn't there anything in for loop like "for x = a to c and y = d to g" as we can write in case of if statement "if a = b and c = d"?
 
Upvote 0
No, you cannot do something like
Code:
For x=1 to 10 and y = 1 to 10
 
Upvote 0
You might be looking for sytnax like

Code:
For x = 1 to 10
    For y = 1 to 10
        ' do something
    Next y
Next x
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top