【发布时间】:2019-02-09 06:19:12
【问题描述】:
Sub lol_function()
Dim x, y, count, i, mhr, z, allowed, leftover As Integer
For y = 13 To 210
For x = 6 To 1000 Step 8
allowed = 50 * Cells(x, 8)
mhr = Cells(x, 7)
count = count + mhr
If mhr <= allowed And count <= allowed Then
Cells(x, y).value = mhr
Else
Cells(x, y).value = allowed + mhr - count
y = y + 1
leftover = count - allowed
If leftover <= allowed Then
Cells(x, y).value = leftover
count = leftover
Else
Cells(x, y).value = allowed
leftover = mhr - allowed
y = y + 1
End If
Next x, y
End Sub
如果我不使用第二个 if else 循环,那么代码可以正常工作,但使用第二个 if else 语句我不断收到“Next without For”错误。任何帮助将不胜感激。
另外,如果我包含第二端,如果它弄乱了我的代码。我需要它是
if condition
....
else
....
if condition
.....
else
.....
end if
还有其他方法可以在 else 语句中创建循环吗?
【问题讨论】:
-
Next x, y是一个语法错误(您需要 2 个关闭Next's),并且它缺少上面一行中的End If。 Indenting the code would make this more obvious. -
另外,
Dim x, y, count, i, mhr, z, allowed, leftover As Integer并没有按照您的想法行事。leftover是唯一被设置为整数的变量,所有其他变量都是Variants