Excel 块if不带结束if和下一步不带结束for

Excel 块if不带结束if和下一步不带结束for,excel,vba,Excel,Vba,这是一个大学项目,我需要运行宏,检查15个空格中是否有空格。这需要稍后重复,然后需要在该单元格中输入MEG和学生姓名。我遇到的问题是,下一个让For循环工作的方法可能不在正确的位置或IDK中。它只是拒绝工作,当我取出它们并用End替换它时,如果没有End if错误,End Sub将变成一个块。请帮忙 对于没有结束的if问题,如果使用else if两个词,则每个if都需要相应的结束if。您应该使用elseif one-word变体进行编码。请看这两者之间的区别: Dim GroupChoice,

这是一个大学项目,我需要运行宏,检查15个空格中是否有空格。这需要稍后重复,然后需要在该单元格中输入MEG和学生姓名。我遇到的问题是,下一个让For循环工作的方法可能不在正确的位置或IDK中。它只是拒绝工作,当我取出它们并用End替换它时,如果没有End if错误,End Sub将变成一个块。请帮忙

对于没有结束的if问题,如果使用else if两个词,则每个if都需要相应的结束if。您应该使用elseif one-word变体进行编码。请看这两者之间的区别:

Dim GroupChoice, MEGChoice, StudentName, group1Late, MEG1Late As String

'Making the link variables equal to the cell link for the combo boxes
GroupLink = Range("LateGroup")
MEGlink = Range("LateMEG")
'loop to find which choice the user has picked for their group
If GroupLink = 1 Then
    MsgBox "You have not selected a group", vbOKOnly
    Exit Sub
Else
If GroupLink = 2 Then
    GroupChoice = "1"
Else
If GroupLink = 3 Then
    GroupChoice = "2"
End If
'loop to find which choice the user has picked for their MEG
If MEGlink = 1 Then
    MsgBox "you have not selected a MEG for the new student", vbOKOnly
Else
If MEGlink = 2 Then
    MEGChoice = "A"
Else
If MEGlink = 3 Then
    MEGChoice = "B"
Else
If MEGlink = 4 Then
    MEGChoice = "C"
Else
If MEGlink = 5 Then
    MEGChoice = "D"
Else
If MEGlink = 6 Then
    MEGChoice = "E"
End If

StudentName = Range("Studentname")
Sheets("unit 1").Select

If GroupChoice = 1 Then
    For row = 1 To 15
        group1Late = "A" & row
        MEG1Late = "AD" & row
        If ActiveSheet.Cells(row, 1).Value = "" Then
            Range("group1Late") = StudentName
            Range("MEG1Late") = MEGChoice
        End
    Next
End
End Sub
对于下一个无For问题,您应该使用end if来关闭if语句,而不是像当前那样使用end:

if a = 1 then          if a = 1 then
    b = 2                  b = 2
else                   elseif a = 2 then 
    if a = 2 then          b = 1
        b = 1          else
    else                   b = 0
        b = 0          end if
    end if
end if
End本身用于停止程序

我怀疑这个错误是由for next和if end if的不平衡性质造成的,VB看到它们交错,因为没有end if,所以它假设它在next之后的某个地方出现。

对于if WITH end问题,如果使用else if两个词,每个if都需要相应的end if。您应该使用elseif one-word变体进行编码。请看这两者之间的区别:

Dim GroupChoice, MEGChoice, StudentName, group1Late, MEG1Late As String

'Making the link variables equal to the cell link for the combo boxes
GroupLink = Range("LateGroup")
MEGlink = Range("LateMEG")
'loop to find which choice the user has picked for their group
If GroupLink = 1 Then
    MsgBox "You have not selected a group", vbOKOnly
    Exit Sub
Else
If GroupLink = 2 Then
    GroupChoice = "1"
Else
If GroupLink = 3 Then
    GroupChoice = "2"
End If
'loop to find which choice the user has picked for their MEG
If MEGlink = 1 Then
    MsgBox "you have not selected a MEG for the new student", vbOKOnly
Else
If MEGlink = 2 Then
    MEGChoice = "A"
Else
If MEGlink = 3 Then
    MEGChoice = "B"
Else
If MEGlink = 4 Then
    MEGChoice = "C"
Else
If MEGlink = 5 Then
    MEGChoice = "D"
Else
If MEGlink = 6 Then
    MEGChoice = "E"
End If

StudentName = Range("Studentname")
Sheets("unit 1").Select

If GroupChoice = 1 Then
    For row = 1 To 15
        group1Late = "A" & row
        MEG1Late = "AD" & row
        If ActiveSheet.Cells(row, 1).Value = "" Then
            Range("group1Late") = StudentName
            Range("MEG1Late") = MEGChoice
        End
    Next
End
End Sub
对于下一个无For问题,您应该使用end if来关闭if语句,而不是像当前那样使用end:

if a = 1 then          if a = 1 then
    b = 2                  b = 2
else                   elseif a = 2 then 
    if a = 2 then          b = 1
        b = 1          else
    else                   b = 0
        b = 0          end if
    end if
end if
End本身用于停止程序


我怀疑这个错误是由for next的不平衡性质造成的,如果end if,VB会看到它们交错,因为没有结束if,它假设它在next之后的某个地方出现。

你可以简化逻辑,这里a 1..6是顺序的,就像a..E一样

If GroupChoice = 1 Then
    For row = 1 To 15
       group1Late = "A" & row
       MEG1Late = "AD" & row
       If ActiveSheet.Cells(row, 1).Value = "" Then
           Range("group1Late") = StudentName
           Range("MEG1Late") = MEGChoice
       End If ' <-- HERE '
    Next
End If        ' <-- AND HERE '

你可以简化这里的逻辑a 1..6是有序的,就像a..E一样

If GroupChoice = 1 Then
    For row = 1 To 15
       group1Late = "A" & row
       MEG1Late = "AD" & row
       If ActiveSheet.Cells(row, 1).Value = "" Then
           Range("group1Late") = StudentName
           Range("MEG1Late") = MEGChoice
       End If ' <-- HERE '
    Next
End If        ' <-- AND HERE '
看一看选择。。。案例结束选择语法,以便更好地编写多个if。。。否则如果。。。否则如果。。。结束if语句。查看select。。。案例结束选择语法,以便更好地编写多个if。。。否则如果。。。否则如果。。。结束if语句。