当我浏览代码时,我有一个“End If”,vba 告诉我没有相应的“If”块。如果将代码剪切并粘贴到 Notepad++ 中,错误会发生在第 638 行。在代码下面,我将包含一个摘要版本,以便于查看“条件语句”。我知道可能有更好、更有效的方法来做到这一点,但我正在寻找这个特定代码的问题,并且希望避免在可能的情况下进行完全重写。非常感谢任何有助于找出问题所在的帮助。
这是代码:
Public Sub increments()
'check
Dim main As Worksheet: Set main = ThisWorkbook.Worksheets("main")
Dim pc As Worksheet: Set pc = ThisWorkbook.Worksheets("Programs_Courses")
Dim pso As Worksheet: Set pso = ThisWorkbook.Worksheets("ps_output")
lastrow = main.Range("A" & main.Rows.Count).End(xlUp).Row
Dim DestRow As Integer
'programs variable declarations
Dim acc_con As Integer, _
bunch of others...
Dim gen_manag_con As Integer, _
bunch of others...
'initialize program variables to 0
acc_con = 0
all other variables also set to 0
DestRow = 2
For i = 2 To lastrow
If main.Cells(i, 1) = main.Cells(i + 1, 1) Then
'Accounting 1
If main.Cells(i, 7) = pc.Cells(3, 3) Then
acc_con = acc_con + 1
more increments
'Accounting 2
ElseIf main.Cells(i, 7) = pc.Cells(4, 3) Then
ton more incrementing but had to cut due to length
End If
'Check for concentrator status
If acc_con = 2 Or _
pl_an_sys_con = 50 Or _
auto_tech_con = 2 Or _
cul_art_con = 2 Or _
dig_art_des_con = 2 Or _
ece_con = 2 Or _
envi_nat_res_con = 2 Or _
gen_manag_con = 2 Or _
graph_comm_con = 2 Or _
health_sci_con = 2 Or _
horti_con = 1 Or _
mark_manag_con = 2 Or _
media_tech_con = 2 Or _
pltw_biomed_sci_con = 2 Or _
pltw_pre_eng_con = 2 Or _
pltw_pre_eng_con = 3 Or _
sports_med_con = 2 Or _
fam_con_sci_con = 2 Or _
prog_soft_dev_con = 2 Then
pc.Activate
term_match = Application.Match(main.Cells(i, 14), pc.Range(Cells(4, 77), Cells(30, 77)), 0)
If IsError(term_match) Then
ElseIf term_match > 0 Then
If IsEmpty(pso.Cells(DestRow, 2)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 2) = "Y"
pso.Cells(DestRow, 6) = pc.Cells(term_match, 78)
ElseIf Not IsEmpty(pso.Cells(DestRow, 2)) And IsEmpty(pso.Cells(DestRow, 8)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 8) = "Y"
pso.Cells(DestRow, 12) = pc.Cells(term_match, 78)
End If
End If
'reset concentrator
If acc_con = 2 Then acc_con = 0
ElseIf pl_an_sys_con = 50 Then pl_an_sys_con = 0
ElseIf auto_tech_con = 2 Then auto_tech_con = 0
ElseIf cul_art_con = 2 Then cul_art_con = 0
ElseIf dig_art_des_con = 2 Then dig_art_des_con = 0
ElseIf ece_con = 2 Then ece_con = 0
ElseIf envi_nat_res_con = 2 Then envi_nat_res_con = 0
ElseIf gen_manag_con = 2 Then gen_manag_con = 0
ElseIf graph_comm_con = 2 Then graph_comm_con = 0
ElseIf health_sci_con = 2 Then health_sci_con = 0
ElseIf horti_con = 1 Then horti_con = 0
ElseIf mark_manag_con = 2 Then mark_manag_con = 0
ElseIf media_tech_con = 2 Then media_tech_con = 0
ElseIf pltw_biomed_sci_con = 2 Then pltw_biomed_sci_con = 0
ElseIf pltw_pre_eng_con = 2 Then pltw_pre_eng_con = 0
ElseIf pltw_pre_eng_con = 3 Then pltw_pre_eng_con = 0
ElseIf sports_med_con = 2 Then sports_med_con = 0
ElseIf fam_con_sci_con = 2 Then fam_con_sci_con = 0
ElseIf prog_soft_dev_con = 2 Then prog_soft_dev_con = 0
End If
'Check for completer status
ElseIf acc_comp >= 21 And acc_comp <= 25 Or _
pl_an_sys_comp = 50 Or _
pl_an_sys_comp = 60 Or _
auto_tech_comp >= 21 And auto_tech_comp <= 23 Or _
cul_art_comp >= 21 And cul_art_comp <= 25 Or _
dig_art_des_comp >= 21 And dig_art_des_comp <= 23 Or _
ece_comp >= 31 And ece_comp <= 37 Or _
envi_nat_res_comp = 40 Or _
envi_nat_res_comp = 50 Or _
gen_manag_comp >= 21 And gen_manag_comp <= 26 Or _
graph_comm_comp >= 21 Or graph_comm_comp <= 23 Or _
health_sci_comp >= 21 Or health_sci_comp <= 28 Or _
horti_comp = 40 Or _
horti_comp = 50 Or _
mark_manag_comp >= 21 And mark_manag_comp <= 24 Or _
mark_manag_comp = 30 Or _
media_tech_comp >= 21 And media_tech_comp <= 23 Or _
pltw_biomed_sci_comp >= 21 And pltw_biomed_sci_comp <= 26 Or _
pltw_pre_eng_comp >= 22 And pltw_pre_eng_comp <= 26 Or _
sports_med_comp >= 21 And sports_med_comp <= 27 Or _
fam_con_sci_comp >= 21 And fam_con_sci_comp <= 26 Or _
fam_con_sci_comp = 30 Or _
prog_soft_dev_comp >= 21 And prog_soft_dev_comp <= 25 Then
pc.Activate
term_match = Application.Match(main.Cells(i, 14), pc.Range(Cells(4, 77), Cells(30, 77)), 0)
If IsError(term_match) Then
ElseIf term_match > 0 Then
If IsEmpty(pso.Cells(DestRow, 3)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 3) = "Y"
pso.Cells(DestRow, 5) = pc.Cells(term_match, 78)
ElseIf Not IsEmpty(pso.Cells(DestRow, 3)) And IsEmpty(pso.Cells(DestRow, 9)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 9) = "Y"
pso.Cells(DestRow, 11) = pc.Cells(term_match, 78)
End If
End If
'reset completer
If acc_comp >= 21 And acc_comp <= 25 Then acc_comp = 0
ElseIf pl_an_sys_comp = 50 Or _
pl_an_sys_comp = 60 Then pl_an_sys_comp = 0
ElseIf auto_tech_comp >= 21 And auto_tech_comp <= 23 Then auto_tech_comp = 0
ElseIf cul_art_comp >= 21 And cul_art_comp <= 25 Then cul_art_comp = 0
ElseIf dig_art_des_comp >= 21 And dig_art_des_comp <= 23 Then dig_art_des_comp = 0
ElseIf ece_comp >= 31 And ece_comp <= 37 Then ece_comp = 0
ElseIf envi_nat_res_comp = 40 Or _
envi_nat_res_comp = 50 Then envi_nat_res_comp = 0
ElseIf gen_manag_comp >= 21 And gen_manag_comp <= 26 Then gen_manag_comp = 0
ElseIf graph_comm_comp >= 21 Or graph_comm_comp <= 23 Then graph_comm_comp = 0
ElseIf health_sci_comp >= 21 Or health_sci_comp <= 28 Then health_sci_comp = 0
ElseIf horti_comp = 40 Or _
horti_comp = 50 Then horti_comp = 0
ElseIf mark_manag_comp >= 21 And mark_manag_comp <= 24 Or _
mark_manag_comp = 30 Then mark_manag_comp = 0
ElseIf media_tech_comp >= 21 And media_tech_comp <= 23 Then media_tech_comp = 0
ElseIf pltw_biomed_sci_comp >= 21 And pltw_biomed_sci_comp <= 26 Then pltw_biomed_sci_comp = 0
ElseIf pltw_pre_eng_comp >= 22 And pltw_pre_eng_comp <= 26 Then pltw_pre_eng_comp = 0
ElseIf sports_med_comp >= 21 And sports_med_comp <= 27 Then sports_med_comp = 0
ElseIf fam_con_sci_comp >= 21 And fam_con_sci_comp <= 26 Or _
fam_con_sci_comp = 30 Then fam_con_sci_comp = 0
ElseIf prog_soft_dev_comp >= 21 And prog_soft_dev_comp <= 25 Then prog_soft_dev_comp = 0
End If
End If
Else
'Accounting 1
If main.Cells(i, 7) = pc.Cells(3, 3) Then
acc_con = acc_con + 1
more increments
'Accounting 2
ElseIf main.Cells(i, 7) = pc.Cells(4, 3) Then
ton more incrementing but had to cut due to length
End If
'Check for concentrator status
If acc_con = 2 Or _
pl_an_sys_con = 50 Or _
auto_tech_con = 2 Or _
cul_art_con = 2 Or _
dig_art_des_con = 2 Or _
ece_con = 2 Or _
envi_nat_res_con = 2 Or _
gen_manag_con = 2 Or _
graph_comm_con = 2 Or _
health_sci_con = 2 Or _
horti_con = 1 Or _
mark_manag_con = 2 Or _
media_tech_con = 2 Or _
pltw_biomed_sci_con = 2 Or _
pltw_pre_eng_con = 2 Or _
pltw_pre_eng_con = 3 Or _
sports_med_con = 2 Or _
fam_con_sci_con = 2 Or _
prog_soft_dev_con = 2 Then
pc.Activate
term_match = Application.Match(main.Cells(i, 14), pc.Range(Cells(4, 77), Cells(30, 77)), 0)
If IsError(term_match) Then
ElseIf term_match > 0 Then
If IsEmpty(pso.Cells(DestRow, 2)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 2) = "Y"
pso.Cells(DestRow, 6) = pc.Cells(term_match, 78)
ElseIf Not IsEmpty(pso.Cells(DestRow, 2)) And IsEmpty(pso.Cells(DestRow, 8)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 8) = "Y"
pso.Cells(DestRow, 12) = pc.Cells(term_match, 78)
Else
End If
End If
'Check for completer status
ElseIf acc_comp >= 21 And acc_comp <= 25 Or _
pl_an_sys_comp = 50 Or _
pl_an_sys_comp = 60 Or _
auto_tech_comp >= 21 And auto_tech_comp <= 23 Or _
cul_art_comp >= 21 And cul_art_comp <= 25 Or _
dig_art_des_comp >= 21 And dig_art_des_comp <= 23 Or _
ece_comp >= 31 And ece_comp <= 37 Or _
envi_nat_res_comp = 40 Or _
envi_nat_res_comp = 50 Or _
gen_manag_comp >= 21 And gen_manag_comp <= 26 Or _
graph_comm_comp >= 21 Or graph_comm_comp <= 23 Or _
health_sci_comp >= 21 Or health_sci_comp <= 28 Or _
horti_comp = 40 Or _
horti_comp = 50 Or _
mark_manag_comp >= 21 And mark_manag_comp <= 24 Or _
mark_manag_comp = 30 Or _
media_tech_comp >= 21 And media_tech_comp <= 23 Or _
pltw_biomed_sci_comp >= 21 And pltw_biomed_sci_comp <= 26 Or _
pltw_pre_eng_comp >= 22 And pltw_pre_eng_comp <= 26 Or _
sports_med_comp >= 21 And sports_med_comp <= 27 Or _
fam_con_sci_comp >= 21 And fam_con_sci_comp <= 26 Or _
fam_con_sci_comp = 30 Or _
prog_soft_dev_comp >= 21 And prog_soft_dev_comp <= 25 Then
pc.Activate
term_match = Application.Match(main.Cells(i, 14), pc.Range(Cells(4, 77), Cells(30, 77)), 0)
If IsError(term_match) Then
ElseIf term_match > 0 Then
If IsEmpty(pso.Cells(DestRow, 3)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 3) = "Y"
pso.Cells(DestRow, 5) = pc.Cells(term_match, 78)
ElseIf Not IsEmpty(pso.Cells(DestRow, 3)) And IsEmpty(pso.Cells(DestRow, 9)) Then
pso.Cells(DestRow, 1) = main.Cells(i, 1)
pso.Cells(DestRow, 9) = "Y"
pso.Cells(DestRow, 11) = pc.Cells(term_match, 78)
Else
End If
End If
End If
'reset all concentrator and completer counts (next line is a new student)
acc_con = 0
acc_comp = 0
pl_an_sys_con = 0
pl_an_sys_comp = 0
auto_tech_con = 0
auto_tech_comp = 0
cul_art_con = 0
cul_art_comp = 0
dig_art_des_con = 0
dig_art_des_comp = 0
ece_con = 0
ece_comp = 0
envi_nat_res_con = 0
envi_nat_res_comp = 0
gen_manag_con = 0
gen_manag_comp = 0
graph_comm_con = 0
graph_comm_comp = 0
health_sci_con = 0
health_sci_comp = 0
horti_con = 0
horti_comp = 0
mark_manag_con = 0
mark_manag_comp = 0
media_tech_con = 0
media_tech_comp = 0
pltw_biomed_sci_con = 0
pltw_biomed_sci_comp = 0
pltw_pre_eng_con = 0
pltw_pre_eng_comp = 0
sport_med_con = 0
sport_med_comp = 0
fam_con_sci_con = 0
fam_con_sci_comp = 0
prog_soft_dev_con = 0
prog_soft_dev_comp = 0
DestRow = pso.Range("A" & main.Rows.Count).End(xlUp).Offset(1, 0).Row
End If
Next i
End Sub
总结如下
Public Sub increments()
for i = 2 to lastrow
if stu_id = stu_id + 1 then
if .....
increment variables
end if
if ..... (check con status)
if .....
if .....
display results
end if
end if
if .....
reset con variables
end if
elseif ..... (check comp status)
if .....
if .....
display results
end if
end if
if .....
reset comp variables
end if
end if 'line the error occurs
else
if .....
increment variables
end if
if ..... (check con status)
if .....
if .....
display results
end if
end if
elseif ..... (check comp status)
if .....
if .....
display results
end if
end if
end if
reset all variables to 0
end if
next i
end sub
提前谢谢您。 真诚的,永远的学生。
有两个地方将单行 If 语句与普通 If 语句混淆了。
Bigben 已经找到了 2 个地方中的 1 个(请参阅评论),另一个是您在代码中有评论的地方
'reset concentrator
您可以查看
If
语句是否是单行如果当您在 Then
关键字之后有代码时:
If acc_con = 2 Then acc_con = 0
该声明是的缩写
If acc_con = 2 Then
acc_con = 0
End If
正如您所注意到的,有一个隐式的 End If
- 换句话说,您不能在新行上为该 If 添加
Else
或
ElseIf
。您可以在same
线上有一个
Else
(但没有
ElseIf
)(但您应该避免这样做)。
If acc_con = 2 Then acc_con = 0 Else acc_con = 99
因此将您的代码更改为
'reset concentrator
If acc_con = 2 Then
acc_con = 0
ElseIf pl_an_sys_con = 50 Then
pl_an_sys_con = 0
ElseIf auto_tech_con = 2 Then
auto_tech_con = 0
ElseIf cul_art_con = 2 Then
(...)
和
'reset completer
If acc_comp >= 21 And acc_comp <= 25 Then
acc_comp = 0
ElseIf pl_an_sys_comp = 50 Or pl_an_sys_comp = 60 Then
pl_an_sys_comp = 0
ElseIf auto_tech_comp >= 21 And auto_tech_comp <= 23 Then
auto_tech_comp = 0
(...)