mercredi 30 novembre 2016

If Statement VBA

I am writing a script in Excel VBA with an If and an ElseIf statements for a database search. The search is conducted through a UserForm that has two fields, labelled as Country and Category and defined in the script as follows:

Dim country As String
Dim Category As String
country = Sheets("Results").Range("D5").Value
Category = Sheets("Results").Range("D6").Value

The information is searched and presented in respect of the country searched and, as such, the minimum required for a search to run is the Country being provided by the user with a country that is in the database.

Taking the user-inputted criteria, the search runs through a table of data in a sheet called Database and pastes the results in another sheet called Results. Depending on the search criteria, the script will run several options prescribed by an If statement.

OPTION 1 - The user has provided a country and a category and:

  • The country exists in the database but;
  • The Category does not exist for the specific country.

In this case a MsgBox will pop up with saying that the specific combination of country and category provided by the user does not exist in the database. The message will ask the user if it would like to run a search just for all entries of the country provided, in this case. I have written the respective code as follows:

finalrow = Sheets("Database").Range("A200000").End(xlUp).Row

For i = 2 To finalrow

    If Sheets("Database").Cells(i, 1) = country And _
        (Category = "" Or 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")
                   If question = vbYes Then
                        Sheets("Results").Range("D6").ClearContents
                        Category = Sheets("Results").Range("D6").Value
                        boolRestart = True
                    Else
                        Sheets("Results").Range("D5").ClearContents
                        Sheets("Results").Range("D6").ClearContents
                        Me.Hide
                        WelcomeForm.Show
                        Exit Sub
                    End If

OPTION 2 - The user has provided a country and:

  • The country exists in the database and;
  • The user has also provided a Category that exists in the database for the specific country or;
  • The user has left the Category field empty.

In this case, the search will run. This is written in the script as follows:

ElseIf Sheets("Database").Cells(i, 1) = country And _
        (Sheets("Database").Cells(i, 3) = Category Or Category = "") 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

    End If

I have tried to write the script in several different ways but the search engine keeps on not working as I want to. What is happening now is that when I input a Country that I know to be in the database, regardless of inputting a Category as well or not, OPTION 1 is always triggered. I have tried to take out OPTION 1 altogether and run just an If statement with OPTION 2 as is and the search runs fine with Country filled in and with both Country and Category filled in. However, as soon as OPTION 1 in in the code, this is always the option ran, regardless of what is inputted by the user.

The full code is here , for your reference:

Dim country As String 'Search query country, user-inputted
Dim Category As String 'Search query category user-inputted
Dim finalrow As Integer
Dim i As Integer 'row counter
Dim ws As Worksheet

Set ws = Sheets("Database")

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

For i = 2 To finalrow

    If Sheets("Database").Cells(i, 1) = country And _
        (Category = "" Or 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")
                   If question = vbYes Then
                        Sheets("Results").Range("D6").ClearContents
                        Category = Sheets("Results").Range("D6").Value
                        boolRestart = True
                    Else
                        Sheets("Results").Range("D5").ClearContents
                        Sheets("Results").Range("D6").ClearContents
                        Me.Hide
                        WelcomeForm.Show
                        Exit Sub
                    End If

    ElseIf Sheets("Database").Cells(i, 1) = country And _
        (Sheets("Database").Cells(i, 3) = Category Or Category = "") 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

    End If

Next I

Thank you very much for your help.

Aucun commentaire:

Enregistrer un commentaire