VBA中的if else循环中是否可以存在if else循环?

时间:2019-02-08 22:13:11

标签: excel vba

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 for For”错误。任何帮助将不胜感激。

如果第二个结尾弄乱了我的代码,也要包括在内。我需要成为

if condition
....

else 
....

       if condition
            .....

       else
            .....

end if 

还有其他方法可以在else语句中创建循环吗?

2 个答案:

答案 0 :(得分:4)

由于有两个“ If”语句,因此在“ Next”语句之前需要第二个“ End If”。

答案 1 :(得分:3)

您需要在第一条CREATE SEQUENCE S1 AS INT START WITH 1; CREATE TABLE T1 ( id INT NOT NULL DEFAULT NEXT VALUE FOR S1, name varchar(50), anchorid INT NOT NULL DEFAULT NEXT VALUE FOR S1 ) INSERT INTO T1 (name) VALUES ('foo'); SELECT * FROM T1 和第二条X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)语句之间插入End If