简体   繁体   English

For..Next语句的无限循环

[英]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. 目标:开发一个宏,以便在sheet1中进行选择,将第十二行中的值除以1.35,然后将其转移到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 ". 诀窍是取出“ do-loop L”。 That is causing the infinite loop, as you are never increasing "L". 这将导致无限循环,因为您永远不会增加“ 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. (使用相同命名变量的for循环,只有在自己的作用域内才具有该变量...然后它将不再是无限的。

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: 如果要将公式设置为Sheet2的单元格 ,请如下修改设置行:

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: 我没有要测试的任何示例数据,因此这是一个调试版本,它将输出到立即窗口(CTRL-G),整行将其编译为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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