IF Excel VBA中的ElseIF语句

时间:2016-11-23 12:37:01

标签: excel vba excel-vba if-statement

我正在尝试为基于国家/地区的数据库搜索编写脚本。搜索是在一个充满数据的工作表上运行的,称为"数据库"并将结果粘贴到另一张名为" Results"。

的表格中

搜索取决于用户输入的变量,我已将其定义为" Country"," Category"和"子类别":

country = Sheets("Results").Range("D5").Value
Category = Sheets("Results").Range("D6").Value
Subcategory = Sheets("Results").Range("D7").Value
finalrow = Sheets("Database").Range("A200000").End(xlUp).Row

我希望用户通过UserForm填写搜索条件以引导不同的方案。就这样:

1 - 如果用户搜索未包含在数据库中的国家/地区,则搜索将不会运行,并且会弹出这样的消息。我使用了.Find函数:

With Worksheets("Database")
        Set c = .Range("A:A").Find(What:=country, After:=.Cells(1, 1), _
                LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False)
        If Not c Is Nothing Then
        Else
            MsgBox "Unfortunately, the database does not have any sources for information in " & country & ". Please search for sources in relating to another country."
            Sheets("Results").Range("D5").ClearContents
            Sheets("Results").Range("D6").ClearContents
            Sheets("Results").Range("D7").ClearContents
            Exit Sub
        End If
    End With

2-如果用户未生成要搜索的国家/地区的名称,则搜索将不会运行。我使用IF语句来执行此操作:

If country = "" Then
        Sheets("Results").Range("B10:J200000").Clear
        MsgBox "You must select a country in order to search the database. Please do so in the drop-down list provided."
        Sheets("Results").Range("D5").ClearContents
        Sheets("Results").Range("D6").ClearContents
        Sheets("Results").Range("D7").ClearContents
        Exit Sub
    End If

3 - 如果用户在数据库中搜索国家/地区,则搜索会运行,列出该国家/地区的所有匹配项,或者,如果用户通过提供"类别"来缩小搜索范围。和"子类别",仅列出符合三个条件的数据库条目。我通过IF语句完成了这一操作,与第一个语句分开:

For i = 2 To finalrow

    'If the country field is filled in and there results from the search made
    If Sheets("Database").Cells(i, 1) = country And _
        (Sheets("Database").Cells(i, 3) = Category Or Category = "") And _
        (Sheets("Database").Cells(i, 4) = Subcategory Or Subcategory = "") Then

            'Copy the headers of the "Database" sheet
            With Sheets("Database")
            .Range("A1:I1").Copy
            End With
            Sheets("Results").Range("B10:J10").PasteSpecial

            'Copy the rows of the "Database" that match the search query
            With Sheets("Database")
            .Range(.Cells(i, 1), .Cells(i, 9)).Copy
            End With
            Sheets("Results").Range("B20000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats

4 - 作为最后一个场景,我希望脚本能够对用户提供" Country"," Category"和/或"子类别"但是,尽管数据库具有该国家/地区的条目,但该类别或子类别中没有该国家/地区的条目。在这种情况下,我希望脚本询问用户是否要查看搜索到的国家/地区的所有信息,而不管"类别"或"子类别"。我尝试通过IfElse语句执行此操作,该语句是上一个IF语句的一部分:

ElseIf Sheets("Database").Cells(i, 1) = country And _
       (Sheets("Database").Cells(i, 3) <> Category) Then
        Dim question As Integer
        question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you perhaps want to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "No results for your search")
        Sheets("Results").Range("D6").ClearContents
        Sheets("Results").Range("D7").ClearContents
        MsgBox question
            If question = vbYes Then
              Close
              Call SearchButton_Click
              Exit Sub
            Else
              Sheets("Results").Range("D5").ClearContents
              Sheets("Results").Range("D6").ClearContents
              Sheets("Results").Range("D7").ClearContents
              Exit Sub
            End If

这是我希望脚本尽可能考虑的四种情况。

当只有方案1,2和3到位时,搜索和脚本整体运行正常。但是,当我在方案4中插入ElseIf语句的脚本时,出现了问题。虽然方案1和方案2继续工作,但每当我搜索一个我知道的国家/地区时,就会有#34;类别&#34;和&#34;子类别&#34;,脚本提示方案4,好像没有匹配#34;国家&#34;,&#34;类别&#34;和&#34;子类别&#34;。

我不确定我可能做错了什么或我的ElseIf声明出了什么问题。

整码

Private Sub SearchButton_Click()

Dim country As String 'Search query category user-inputted
Dim Category As String 'Search query category user-inputted
Dim Subcategory As String 'Search query category user-inputted
Dim finalrow As Integer
Dim LastRowForTable As Long 'Last filled row in Results table
Dim i As Integer 'row counter
Dim ws As Worksheet

Set ws = Sheets("Database")

'Erase any entries from the Results sheet
Sheets("Results").Range("B10:J200000").ClearContents

'Define the user-inputed variables
country = Sheets("Results").Range("D5").Value
Category = Sheets("Results").Range("D6").Value
Subcategory = Sheets("Results").Range("D7").Value
finalrow = Sheets("Database").Range("A200000").End(xlUp).Row

'If statement for search    
With Worksheets("Database")
    Set c = .Range("A:A").Find(What:=country, After:=.Cells(1, 1), _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
    If Not c Is Nothing Then
    Else
        MsgBox "Unfortunately, the database does not have any sources for information in " & country & ". Please search for sources in relating to another country."
        Sheets("Results").Range("D5").ClearContents
        Sheets("Results").Range("D6").ClearContents
        Sheets("Results").Range("D7").ClearContents
        Exit Sub
    End If
End With

'If the country field is left empty, there is no need to even run a search
If country = "" Then
    Sheets("Results").Range("B10:J200000").Clear
    MsgBox "You must select a country in order to search the database. Please do so in the drop-down list provided."
    Sheets("Results").Range("D5").ClearContents
    Sheets("Results").Range("D6").ClearContents
    Sheets("Results").Range("D7").ClearContents
    Exit Sub
End If

For i = 2 To finalrow

    'If the country field is filled in and there results from the search made
    If Sheets("Database").Cells(i, 1) = country And _
        (Sheets("Database").Cells(i, 3) = Category Or Category = "") And _
        (Sheets("Database").Cells(i, 4) = Subcategory Or Subcategory = "") Then

        'Copy the headers of the "Database" sheet
        With Sheets("Database")
            .Range("A1:I1").Copy
        End With
        Sheets("Results").Range("B10:J10").PasteSpecial

        'Copy the rows of the table that match the search query
        With Sheets("Database")
            .Range(.Cells(i, 1), .Cells(i, 9)).Copy
        End With
        Sheets("Results").Range("B20000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats

    ElseIf Sheets("Database").Cells(i, 1) = country And _
        (Sheets("Database").Cells(i, 3) <> Category) Then

        Dim question As Integer
        question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you perhaps want to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")
        Sheets("Results").Range("D6").ClearContents
        Sheets("Results").Range("D7").ClearContents

        MsgBox question
        If question = vbYes Then
            Close
            Call SearchButton_Click
            Exit Sub
        Else
            Sheets("Results").Range("D5").ClearContents
            Sheets("Results").Range("D6").ClearContents
            Sheets("Results").Range("D7").ClearContents
            Exit Sub
        End If

    End If

Next i

'Hides search form
Me.Hide

'Toggle Results sheet
Sheets("Results").Activate

End Sub

1 个答案:

答案 0 :(得分:0)

valid

好的我做了什么:

停止了sub的结束并召回。

if if msgbox = yes子句中的方案3。这当然要求我们将类别和子类别重置为空白,我也这样做了。