• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Two different loops at same time

Hi,


I'd like to know how can I set up two different kind of loops to running at the same time. I want this:


"For each cell in RANGE and For i=1 to 10" (for example)

Loop would be: cell(1) and i=1 ; cell(2) and i=2 ...


I wrote:

[pre]
Code:
For Each cell In FornecedoresG
valor = Application.WorksheetFunction.Match(cell, FornecedoresH, 0)
If Not (IsEmpty(valor)) Then
sname = FornecedoresH.Cells(1, 1).Offset(valor - 1, 1).value
End If

For i = 2 To ULinhaG
Range(Cells(i, 1), Cells(i, 6)).Select
Selection.Copy
Windows("org_mangaratiba.xlsx").Activate
Sheets(sname).Paste
Windows("despesa_anual.xlsm").Activate
Next

Next
[/pre]

But in this case its cell(1) then all the loop between i=2 and i=lastline ... cell(2) then all the loop between i=2 and i=lastline ... etc


Thanks
 
Would an arrangement like this work?

[pre]
Code:
i = 2
For Each cell In FornecedoresG
valor = Application.WorksheetFunction.Match(cell, FornecedoresH, 0)
If Not (IsEmpty(valor)) Then
sname = FornecedoresH.Cells(1, 1).Offset(valor - 1, 1).Value
End If

Range(Cells(i, 1), Cells(i, 6)).Copy
Windows("org_mangaratiba.xlsx").Activate
Sheets(sname).Paste
Windows("despesa_anual.xlsm").Activate
i = i + 1
If i > ULinhaG Then Exit For
Next
[/pre]
No we only increment i on each main loop, and we still exit the process after reaching the ULinhaG value.
 
Back
Top