For Loop

H

Harish

Hi,

I am trying to write a code such that a parameter j has to iterate
continuously but the parameter k=2 has to restart everytime j =41 or
multiples of it. Here is an example

j k
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 2
22 3
23 4
24 5
25 6
26 7
27 8
28 9
29 10
30 11
31 12
32 13
33 14
34 15
35 16
36 17
37 18
38 19
39 20
40 21
41 2
42 3
43 4
44 5
45 6
46 7
47 8
48 9
49 10
50 11
51 12
52 13
53 14
54 15
55 16
56 17
57 18
58 19
59 20
60 21
61 2
62 3
63 4
64 5
65 6
66 7
67 8
68 9
69 10
70 11
71 12
72 13
73 14
74 15
75 16
76 17
77 18
78 19
79 20
80 21
 
H

Harish

Hi,

In my earlier post, i meant j = 21 and NOT multiples of it. k should
restart everytime j =21, 41, 61 and so on. Also this is the code which
I tried and is not working:

For j = 1 to 81
 
H

Harish

i=1
j=1

For j = 1 to 81

if j <=20 Then
k = j+1
Else
k = j*mod(21)+2
End if
j=j+1
i=i+1
Cells(i, 3) = k
Next
 
G

Gary Keramidas

see if this does what you want

Sub test()
i = 1
j = 1

For j = 1 To 81
If j Mod 20 <> 0 Then
Range("A" & j).Value = j
Range("B" & j).Value = i + 1
Else
Range("A" & j).Value = j
Range("B" & j).Value = i + 1
i = 0
End If
i = i + 1
Next
End Sub
 
M

Mike

Sub test()
innerloopCellCounter = 1
For j = 1 To 82
Range("A" & j).Value = j
If j Mod 41 = 0 Then
For i = 1 To 41
Range("B" & innerloopCellCounter).Value = i
innerloopCellCounter = innerloopCellCounter + 1
Next i
End If
Next j
End Sub
 
M

Mike

Try tis one instead
Sub test()
innerloopCellCounter = 1
For j = 1 To 80
Range("A" & j).Value = j
If j Mod 21 = 1 Then
For i = 2 To 21
Range("B" & innerloopCellCounter).Value = i
innerloopCellCounter = innerloopCellCounter + 1
Next i
End If
Next j
End Sub
 
H

Harish

Hi there,

I am unable to understand your solution here. Does your code ensures
that i restarts from 2 everytime j = 21, 41, 61 etc? Thanks for your
help.
 
H

Harish

Hi Gary,

your logic works correctly. I work with a automated testing software
and I want to use your logic in that software. This software uses VB
scripting language. I tried to implement your logic in it but it
doesnt work. I want to mention that j = 1 to 20 for some time and then
changes to j = 21 to 40 and then changes to 41 to 60. but k has to
restart everytime j = 21, 41, 61 and so on....Here is my code and I
would appreciate if you can correct the logic behind:

For j = firstnum to secondnum
If j mod(row_count-1) <>0 Then
k=j+1
invoice_array(j) = Browser("").Page("_3").Frame
("_sweview_3").WebTable("Select for MultiPay").GetCellData(k,5)
Else
k=j+1
invoice_array(j) = Browser("").Page("_3").Frame("_sweview_3").WebTable
("Select for MultiPay").GetCellData(k,5)
k=0
End If
End if
k=k+1
Next

firstnum and second are variables that keeps changing like i said
before (1-20, 21-40, 41-60).

Thanks for your help
 
G

Gary Keramidas

sorry, not sure.

what if you change this:
If j mod(row_count-1) <>0 Then
to this:
If j mod(20) <>0 Then
 
J

JLGWhiz

Hi Harish, see if this does what you want.

Sub terfuge()
Dim j, k

j = 1
k = 1

With ActiveSheet
For j = 1 To 80
.Cells(j, 1) = j
.Cells(j, 2) = k + 1
k = k + 1
If j > 1 And j Mod 20 = 0 Then
k = 1
End If
Next
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top