VBA - elseif 语句跳过 "ElseIf"

VBA - elseif statement skipping "ElseIf"

在以下代码中,代码有效,但跳过了 "else if" 行:ElseIf y = "BAS" then "Basingstoke"

由于贝辛斯托克在 Y 的来源数组中位于贝尔法斯特之后,因此它保留 "Belfast" 作为 "Region" 值。它工作正常,但只是完全跳过 ElseIf 行并在之后立即转到 "end if" 。

我有一种强烈的感觉,我在这里遗漏了一些明显的东西...

For x = MinDate To MaxDate
    With salesr
        Set FindRng = .Find(What:=x, _
                            LookIn:=xlValues, _
                            Lookat:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not FindRng Is Nothing Then
                RowNumber = FindRng.Row
                FindRng.Offset(, 2).Resize(1, 39).ClearContents
                For Each y In RegionsArray
                    With Regions
                        Set FindRng2 = .Find(What:=y, _
                                             LookIn:=xlValues, _
                                             Lookat:=xlWhole, _
                                             SearchOrder:=xlByColumns, _
                                             SearchDirection:=xlNext, _
                                             MatchCase:=False)
                             If Not FindRng2 Is Nothing Then
                                ColLeft = FindRng2.Column
                                ColRight = FindRng2.Column + 1
                                result = Application.WorksheetFunction.SumIfs(Import.Range("S:S"), Import.Range("M:M"), x, Import.Range("O:O"), y)
                                result1 = Application.WorksheetFunction.SumIfs(Import.Range("T:T"), Import.Range("M:M"), x, Import.Range("O:O"), y)
                                If result > 0 Then
                                    sales.Cells(RowNumber, ColLeft).Value = result
                                Else
                                End If
                                sales.Cells(RowNumber, ColRight + 1).Value = result1
                                If y = "BLF" Or y = "BAS" Then
                                    If y = "BLF" Then Region = "Belfast"
                                    ElseIf y = "BAS" Then Region = "Basingstoke"
                                    End If
                                    If y <> "BLF" And y <> "BAS" Then
                                    Region = y
                                End If
                             End If
                             sales.Cells(RowNumber, ColRight).Formula = "=SUMIFS(Table_PTQV02_PTQSOFT_SALES_ORDERS_Index3[Sales_day],Table_PTQV02_PTQSOFT_SALES_ORDERS_Index3[SALES_DATE],""" & x & """,Table_PTQV02_PTQSOFT_SALES_ORDERS_Index3[REGION_NAME],""*" & Region & "*"")"                
                    End With
                Next y
            Else
            End If
    End With                         
Next x

此代码块无效:

   If y = "BLF" Or y = "BAS" Then
       If y = "BLF" Then Region = "Belfast"
       ElseIf y = "BAS" Then Region = "Basingstoke"
       End If
       If y <> "BLF" And y <> "BAS" Then
       Region = y
   End If

尝试像这样构造它:

   If y = "BLF" Or y = "BAS" Then
       If y = "BLF" Then
          Region = "Belfast"
       ElseIf y = "BAS" Then
          Region = "Basingstoke"
       End If
   ElseIf y <> "BLF" And y <> "BAS" Then
       Region = y
   End If

或者您可以像这样彻底简化它:

   Region = y
   If y = "BLF" Then Region = "Belfast"
   If y = "BAS" Then Region = "Basingstoke"

保持条件简单,写成如下:

If y = "BLF" Or y = "BAS" Then
   If y = "BLF" Then
      Region = "Belfast"
   Else
      Region = "Basingstoke"
   End If
Else
    Region = y     
End If