Когда я просматриваю код, у меня есть «Конец, если», о котором vba сообщает мне, что у него нет соответствующего блока «Если». Если вы вырежете и вставите код в 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
Заранее спасибо. С уважением, вечный студент.
@Warcupine У меня было ощущение, что это так. Я просто не могу понять, какие из них VBA считает одиночными.
Во-первых: используйте регистр Select вместо всех ваших if и т. д., где вы смотрите на одно и то же значение - например. main.Cells(i, 7), но в большинстве случаев вы никогда не поймете свой код за один месяц, а кто-то другой вообще не поймет. Назовите свои переменные понятными именами. И проверьте условия, которые нужно разделить, и действительно ли они так нужны. Это пахнет со всеми этими или
@BigBen Я понимаю вашу ссылку, потому что ее легко увидеть в одной строке, если операторы if не нуждаются в конце if. Я уверен, что мне не хватает чего-то простого, но я все еще этого не вижу.
Это неверно: If acc_comp >= 21 And acc_comp <= 25 Then acc_comp = 0
, за которым сразу следует ElseIf pl_an_sys_comp = 50 Or _
. Первый — это однострочный оператор.
Мне придется пересмотреть эти части и переписать их, чтобы они были действительными, как только я смогу взглянуть на это снова. Спасибо за подсказки. @Биг Бен
У вас есть два места, где вы путаете однострочные операторы If с обычными операторами If.
Бигбен уже нашел одно из двух мест (см. комментарии), второе — там, где у вас есть комментарий в коде 'reset concentrator
Вы можете увидеть, является ли оператор If
однострочным, если у вас есть код после ключевого слова Then
:
If acc_con = 2 Then acc_con = 0
Это утверждение является сокращением от
If acc_con = 2 Then
acc_con = 0
End If
Как вы заметили, здесь присутствует неявный End If
— другими словами, вы не можете использовать Else
или ElseIf
для этого If в новой строке. В одной строке может быть 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
(...)
Спасибо. Очень ясно и лаконично. Вечером я переработаю код и отмечу ответ. Еще раз спасибо.
Вы смешиваете многострочные операторы if и однострочные операторы if.