当if语句用于打开其他工作簿时,ElseIf函数不起作用

时间:2019-01-11 13:36:18

标签: excel vba

我正在使用If / ElseIf语句根据单元格中的值打开不同的excel工作簿,因此如果某个值有问题,则会打开相应的文档。

    If ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "FWS Mag Seal replacement" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D FWS MAG SEAL REPLACEMENT.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "M04 S/E" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B M04 REMOVA & REPLACE.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal replacement" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D output shaft mag seal replacement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal & Rear Bearing descaling" Then Workbooks.Open ""
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Sealing Bush replacement" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D Sealing bush replacement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 180" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\arriel 2b tu180.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 181 - TU 198" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B TU181-198.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 201" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU201 Parts requirement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 213" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\Arriel 2E TU213.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 215" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU215 rev1.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU213-215 (inc. Consumables)" Then Workbooks.Open ""
    End If

由于某种原因,在if语句的第1行之后,如果该值不是“ FWS Mag Seal替换”,则它将直接转到if语句的末尾,并点燃所有其他elseif语句。

3 个答案:

答案 0 :(得分:2)

在这种情况下,我更喜欢使用Case Select函数。更清楚了,整理了一下代码:

Select Case ThisWorkbook.Sheets("Parts_List").Range("E2").Value
    Case "FWS Mag Seal replacement"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D FWS MAG SEAL REPLACEMENT.xlsx"
    Case "M04 S/E"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B M04 REMOVA & REPLACE.xlsx"
    Case "Power Output Shaft Mag seal replacement"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D output shaft mag seal replacement.xlsx"
    Case "Power Output Shaft Mag seal & Rear Bearing descaling"
        Workbooks.Open ""
    Case "Sealing Bush replacement"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D Sealing bush replacement.xlsx"
    Case "TU 180"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\arriel 2b tu180.xlsx"
    Case "TU 181 - TU 198"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B TU181-198.xlsx"
    Case "TU 201"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU201 Parts requirement.xlsx"
    Case "TU 213"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\Arriel 2E TU213.xlsx"
    Case "TU 215"
        Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU215 rev1.xlsx"
    Case "TU213-215 (inc. Consumables)"
        Workbooks.Open ""
End Select

如果您仍然喜欢使用If Case函数,我建议您编写这样的代码(您只是忘记在每个:之后放置Then):

If ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "FWS Mag Seal replacement" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D FWS MAG SEAL REPLACEMENT.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "M04 S/E" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B M04 REMOVA & REPLACE.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal replacement" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D output shaft mag seal replacement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal & Rear Bearing descaling" Then: Workbooks.Open ""
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Sealing Bush replacement" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D Sealing bush replacement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 180" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\arriel 2b tu180.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 181 - TU 198" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B TU181-198.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 201" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU201 Parts requirement.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 213" Then Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\Arriel 2E TU213.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 215" Then: Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU215 rev1.xlsx"
    ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU213-215 (inc. Consumables)" Then: Workbooks.Open ""
End If

答案 1 :(得分:1)

您需要稍微调整一下语法。您正在将单行If语句与多行If语句和vba合并在一起。尝试像这样格式化:

If ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "FWS Mag Seal replacement" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D FWS MAG SEAL REPLACEMENT.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "M04 S/E" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B M04 REMOVA & REPLACE.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal replacement" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D output shaft mag seal replacement.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Power Output Shaft Mag seal & Rear Bearing descaling" Then
   Workbooks.Open ""
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "Sealing Bush replacement" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2D Sealing bush replacement.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 180" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\arriel 2b tu180.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 181 - TU 198" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2B TU181-198.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 201" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU201 Parts requirement.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 213" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\Arriel 2E TU213.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU 215" Then
   Workbooks.Open "C:\Users\UE294839\Documents\Off Site Project\Parts Lists\ARRIEL 2E TU215 rev1.xlsx"
ElseIf ThisWorkbook.Sheets("Parts_List").Range("E2").Value = "TU213-215 (inc. Consumables)" Then
   Workbooks.Open ""
End If

答案 2 :(得分:0)

将ElseIf用作下一个

代码

Sub SelectCaseInsteadOfElseIf()

    Const cBooks As String = _
            "ARRIEL 2D FWS MAG SEAL REPLACEMENT.xlsx" _
            & "," & "ARRIEL 2B M04 REMOVA & REPLACE.xlsx" _
            & "," & "ARRIEL 2D output shaft mag seal replacement.xlsx" _
            & "," & "" _
            & "," & "ARRIEL 2D Sealing bush replacement.xlsx" _
            & "," & "arriel 2b tu180.xlsx" _
            & "," & "ARRIEL 2B TU181-198.xlsx" _
            & "," & "ARRIEL 2E TU201 Parts requirement.xlsx" _
            & "," & "Arriel 2E TU213.xlsx" _
            & "," & "ARRIEL 2E TU215 rev1.xlsx" _
            & "," & ""

    Const cCriteria As String = _
            "FWS Mag Seal replacement" _
            & "," & "M04 S/E" _
            & "," & "Power Output Shaft Mag seal replacement" _
            & "," & "Power Output Shaft Mag seal & Rear Bearing descaling" _
            & "," & "Sealing Bush replacement" _
            & "," & "TU 180" _
            & "," & "TU 181 - TU 198" _
            & "," & "TU 201" _
            & "," & "TU 213" _
            & "," & "TU 215" _
            & "," & "TU213-215 (inc. Consumables)"

    Const cPath As String = "C:\Users\UE294839\Documents\Off Site Project\" _
            & "Parts Lists\"
    Const cSheet As String = "Parts_List"
    Const cRange As String = "E2"

    Dim vntB As Variant
    Dim vntC As Variant
    Dim i As Integer

    vntB = Split(cBooks, ",")
    vntC = Split(cCriteria, ",")

    For i = 0 To UBound(vntC)
        If ThisWorkbook.Sheets(cSheet).Range(cRange).Value = vntC(i) Then
            Workbooks.Open cPath & vntB(i)
            Exit For
        End If
    Next

End Sub