gpt4 book ai didi

excel - ElseIf 在不应该触发时触发

转载 作者:行者123 更新时间:2023-12-04 21:32:15 25 4
gpt4 key购买 nike

我正在编写一个脚本,允许用户按名称或数字输入月份。在下面的语句中,我可以在输入框和行中输入任何我想要的内容:

    ElseIf sMonthOfMaintenance = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "8" Or "9" Or "10" Or "11" Or "12" Then
sMonthOfMaintenanceNumber = sMonthOfMaintenanceNumber

总会触发。我可以在输入框中键入“SREGBswerbwergv”,它仍然会触发该行并且不会移动到“Else”语句。我重新启动了 excel 以防万一,它仍然可以,但如果我注释掉该语句,则不会。完整代码:
'...and date of report.[BD]
sMonthOfMaintenance = InputBox("What month are you reveiwing?")

'Ending Wizard if Cancelled [BD]
If sMonthOfMaintenance = "" Then

MsgBox "Operation cancelled"
End

End If

'Converting Month to Number for Output File Name [BD]
If UCase(sMonthOfMaintenance) = "JANUARY" Or UCase(sMonthOfMaintenance) = "JAN" Or UCase(sMonthOfMaintenance) = "JAN" Then
sMonthOfMaintenanceNumber = "01"
ElseIf UCase(sMonthOfMaintenance) = "FEBRUARY" Or UCase(sMonthOfMaintenance) = "FEB" Or UCase(sMonthOfMaintenance) = "FEB." Then
sMonthOfMaintenanceNumber = "02"
ElseIf UCase(sMonthOfMaintenance) = "MARCH" Or UCase(sMonthOfMaintenance) = "MAR" Or UCase(sMonthOfMaintenance) = "MAR." Then
sMonthOfMaintenanceNumber = "03"
ElseIf UCase(sMonthOfMaintenance) = "APRIL" Or UCase(sMonthOfMaintenance) = "APR" Or UCase(sMonthOfMaintenance) = "APR." Then
sMonthOfMaintenanceNumber = "04"
ElseIf UCase(sMonthOfMaintenance) = UCase(sMonthOfMaintenance) = "MAY" Or UCase(sMonthOfMaintenance) = "MAY." Then
sMonthOfMaintenanceNumber = "05"
ElseIf sMonthOfMaintenance = "JUNE" Or UCase(sMonthOfMaintenance) = "JUN" Or UCase(sMonthOfMaintenance) = "JUN." Then
sMonthOfMaintenanceNumber = "06"
ElseIf sMonthOfMaintenance = "JULY" Or UCase(sMonthOfMaintenance) = "JUL" Or UCase(sMonthOfMaintenance) = "JUL." Then
sMonthOfMaintenanceNumber = "07"
ElseIf sMonthOfMaintenance = "AUGUST" Or UCase(sMonthOfMaintenance) = "AUG" Or UCase(sMonthOfMaintenance) = "AUG." Then
sMonthOfMaintenanceNumber = "08"
ElseIf sMonthOfMaintenance = "SEPTEMBER" Or UCase(sMonthOfMaintenance) = "SEP" Or UCase(sMonthOfMaintenance) = "SEP." Then
sMonthOfMaintenanceNumber = "09"
ElseIf sMonthOfMaintenance = "OCTOBER" Or UCase(sMonthOfMaintenance) = "OCT" Or UCase(sMonthOfMaintenance) = "OCT." Then
sMonthOfMaintenanceNumber = "10"
ElseIf sMonthOfMaintenance = "NOVEMBER" Or UCase(sMonthOfMaintenance) = "NOV" Or UCase(sMonthOfMaintenance) = "NOV." Then
sMonthOfMaintenanceNumber = "11"
ElseIf sMonthOfMaintenance = "DECEMBER" Or UCase(sMonthOfMaintenance) = "DEC" Or UCase(sMonthOfMaintenance) = "DEC." Then
sMonthOfMaintenanceNumber = "12"
ElseIf sMonthOfMaintenance = "01" Or "02" Or "03" Or "04" Or "05" Or "06" Or "07" Or "08" Or "09" Or "10" Or "11" Or "12" Then
sMonthOfMaintenanceNumber = sMonthOfMaintenanceNumber
Else
MsgBox "Please Enter a Valid Month"
Call Main
End If

最佳答案

您每次都需要像上面的其他条件一样重复整个条件,例如,sMonthOfMaintenance = "01"或 sMonthOfMaintenance = "02"... 现在,“02”的计算结果为 TRUE。尝试使用 SELECT CASE 来缩短语法。我还看到您试图在最后将数字分配给自己,我认为您不是有意的。这是所有这些的示例:

Sub test()
Dim sMonthOfMaintenance As String
Dim sMonthOfMaintenanceNumber As String

sMonthOfMaintenance = InputBox("What month are you reveiwing?")

'Ending Wizard if Cancelled [BD]
If sMonthOfMaintenance = "" Then
MsgBox "Operation cancelled"
GoTo exit_point
End If

'Converting Month to Number for Output File Name [BD]
Select Case UCase(sMonthOfMaintenance)
Case "JANUARY", "JAN", "JAN."
sMonthOfMaintenanceNumber = "01"
Case "FEBRUARY", "FEB", "FEB."
sMonthOfMaintenanceNumber = "02"
'...
Case "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
sMonthOfMaintenanceNumber = sMonthOfMaintenance
Case Else
MsgBox "Please Enter a Valid Month"
'Call Main
End Select
Debug.Print sMonthOfMaintenanceNumber
exit_point:
End Sub

关于excel - ElseIf 在不应该触发时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49222117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com