简体   繁体   中英

Endless loop with For..Next statement

Objective: Develop a Macro whereby I select in sheet1, divide the values in every twelfth row by 1.35 and transfer them into a selected range of cells in sheet2.

I started simple and it worked:

Worksheets("Sheet2").Cells(114, 12).Formula = Worksheets("Sheet1").Cells(58, 14) / 1.35
Worksheets("Sheet2").Cells(115, 12).Formula = Worksheets("Sheet1").Cells(70, 14) / 1.35
Worksheets("Sheet2").Cells(116, 12).Formula = Worksheets("Sheet1").Cells(82, 14) / 1.35
Worksheets("Sheet2").Cells(117, 12).Formula = Worksheets("Sheet1").Cells(94, 14) / 1.35
Worksheets("Sheet2").Cells(118, 12).Formula = Worksheets("Sheet1").Cells(106, 14) / 1.35
Worksheets("Sheet2").Cells(119, 12).Formula = Worksheets("Sheet1").Cells(118, 14) / 1.35
Worksheets("Sheet2").Cells(120, 12).Formula = Worksheets("Sheet1").Cells(130, 14) / 1.35
Worksheets("Sheet2").Cells(121, 12).Formula = Worksheets("Sheet1").Cells(142, 14) / 1.35

I then wanted to breakdown the code by using a loop, but cannot get it right.

Here is what I have:

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Do
i = 12
j = 14
For l = 114 To 121
For k = 58 To 142 Step 12
Worksheets("Sheet2").Cells(l, i).Formula = Worksheets("Sheet1").Cells(k, j) / 1.35
Next k
Next l
Loop Until l = 121

My ultimate goal is to repeat this loop across different columns as the data comes in.

我将使用索引来执行此操作=INDEX(Sheet1!$N$58:$N$142,(ROWS($A$114:$A114)-1)*12)/1.35请使N为您试图除以的列将其放在您要填充sheet2中的单元格中

the trick is to take out the "do - loop L ". That is causing the infinite loop, as you are never increasing "L". (the for loop which uses the same named variable, has the variable only within his own scope... then it won't be infinite anymore.

Try as follow:

Dim k, l As Integer

'Set before row
k = 46

For l = 114 To 121 Step 1
    Sheets("Sheet2").Cells(l, 12) = Sheets("Sheet1").Cells(k + 12, 14) / 1.35
Next l

I am not clear with your requirement, means, don't know what you want to insert (formula or value).

if you want to set formula to Sheet2's cells , modify the setting line as follow:

Sheets("Sheet2").Cells(l, 12).Formula  = "=" & Sheets("Sheet1").Cells(k + 12, 14).Address & "/1.35"

This should work:

Sub Temp2()
    Dim X As Long, ReadCol As Long, UpdateCode As Long
    ReadCol = 14
    UpdateCol = 12
    For X = 114 To 121
        Worksheets("Sheet2").Cells(X, UpdateCol).Formula = Worksheets("Sheet1").Cells(58 + ((X - 114) * 12), ReadCol) / 1.35
    Next
End Sub

I don't have any sample data to test so here is a debug version that will output to the immediate window (CTRL-G) what the complete line compiles to:

Sub Temp2()
    Dim X As Long, ReadCol As Long, UpdateCode As Long
    ReadCol = 14
    UpdateCol = 12
    For X = 114 To 121
        Debug.Print "Worksheets(""Sheet2"").Cells(" & X & ", " & UpdateCol & ").Formula = Worksheets(""Sheet1"").Cells(" & 58 + ((X - 114) * 12) & ", " & ReadCol & ") / 1.35"
    Next
End Sub

results:

Worksheets("Sheet2").Cells(114, 12).Formula = Worksheets("Sheet1").Cells(58, 14) / 1.35
Worksheets("Sheet2").Cells(115, 12).Formula = Worksheets("Sheet1").Cells(70, 14) / 1.35
Worksheets("Sheet2").Cells(116, 12).Formula = Worksheets("Sheet1").Cells(82, 14) / 1.35
Worksheets("Sheet2").Cells(117, 12).Formula = Worksheets("Sheet1").Cells(94, 14) / 1.35
Worksheets("Sheet2").Cells(118, 12).Formula = Worksheets("Sheet1").Cells(106, 14) / 1.35
Worksheets("Sheet2").Cells(119, 12).Formula = Worksheets("Sheet1").Cells(118, 14) / 1.35
Worksheets("Sheet2").Cells(120, 12).Formula = Worksheets("Sheet1").Cells(130, 14) / 1.35
Worksheets("Sheet2").Cells(121, 12).Formula = Worksheets("Sheet1").Cells(142, 14) / 1.35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM