VBA - elseif语句跳过“ElseIf”

时间:2017-10-26 17:03:35

标签: vba if-statement nested-if

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

由于贝辛斯托克跟随贝尔法斯特进入Ys的阵列,它将“贝尔法斯特”保留为“区域”值。它工作正常,但只是完全跳过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

2 个答案:

答案 0 :(得分:4)

此代码块无效:

   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"

答案 1 :(得分:0)

保持条件简单,并按如下所示编写:

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