如果没有结束,则阻止if和next

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 

这是一个大学项目,我需要macros运行并检查15个空的空间(这将需要稍后重复),然后将需要inputMEG和学生名称到该单元格。 我遇到的问题是, Next为了得到For Loop工作可能不是在正确的地方或什么IDK。 它只是拒绝工作,当我把它们取出并用End来代替时, block if without end if错误block if without end if End ,则End Su b成为block if without end if 。 请帮忙。

对于if-without-end问题,如果使用else if (两个单词),则 if需要相应的end if 。 你应该使用elseif (一个字)变体的方式来编码。 看到这两者之间的区别:

 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 

对于next-without-for问题,你应该使用end ifclosures你的if语句,而不是像以前那样end

 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 ' 

自行End用于停止程序。

我怀疑这个错误是由于你的for-nextif-end if的不平衡性质造成的, if-end if VB看到它们是交错的,因为它没有end if所以假设它next

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

 If MEGlink = 1 Then MsgBox "you have not selected a MEG for the new student", vbOKOnly Else MEGChoice = Chr$(63 + MEGlink) '//generate character End If