I am writing a script that runs a Database search. It does so with an IF statement that bears several conditions, which are dependent on user input in three different fields ("country", "category" and "subcategory"). I have defined these previously.
Different combinations of the input by the user in the three fields will produce different outcomes. As such, for instance, if the user does not provide a "country", then the search will not run and an error message will pop up, as follows:
For i = 2 To finalrow
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
If the user provides a country, then the search runs, showing more or less results depending on the user having or having not provided also a category and subcategory:
ElseIf 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
With Sheets("Database")
.Range("A1:I1").Copy
End With
Sheets("Results").Range("B10:J10").PasteSpecial
With Sheets("Database")
.Range(.Cells(i, 1), .Cells(i, 9)).Copy
End With
Sheets("Results").Range("B600").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next I
This is working all fine.
I now want to add more conditions to the IF statement for two additional cases:
1 - The user provides a country that does not exist in the database. I have written this as follows:
ElseIf Sheets("Database").Cells(i, 1) <> country Then
MsgBox "There is no such country in the database. Please search for information relating to another country."
Sheets("Results").Range("D5").ClearContents
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Exit Sub
2: The user provides a combination of a country and database that does not exist in the database. I have written this as follows:
ElseIf (Sheets("Database").Cells(i, 1) = country) And _
(Sheets("Database").Cells(i, 3) <> category) Then
MsgBox "There are no records in the database that match your search criteria. Please try another search"
Sheets("Results").Range("D5").ClearContents
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Exit Sub
As I said, without these two additional ElseIf statements, the script is running fine and the results from the database search are presented. However, when I add these two statements, only the MsgBox from the third If statement (reading "There is no such country in the database. Please search for information relating to another country.") shows.
The entire code, with the two additional statements, is as follows:
For i = 2 To finalrow
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
ElseIf 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
With Sheets("Database")
.Range("A1:I1").Copy
End With
Sheets("Results").Range("B10:J10").PasteSpecial
With Sheets("Database")
.Range(.Cells(i, 1), .Cells(i, 9)).Copy
End With
Sheets("Results").Range("B600").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
Me.Hide
ElseIf Sheets("Database").Cells(i, 1) <> country Then
MsgBox "There is no such country in the database. Please search for information relating to another country."
Sheets("Results").Range("D5").ClearContents
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Exit Sub
ElseIf (Sheets("Database").Cells(i, 1) = country) And _
(Sheets("Database").Cells(i, 3) <> category) Then
MsgBox "There are no records in the database that match your search criteria. Please try another search"
Sheets("Results").Range("D5").ClearContents
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Exit Sub
End If
Next I
Do you have any idea of what I might be doing wrong when I add the two additional statements? Thank you for the help.
country? Is seems that this is merely aStingcontaining one country. If that's correct then you are pretty much allowing for no more than one country in your code:ElseIf Sheets("Database").Cells(i, 1) = country And _. So,Cells(i, 1)must contain exactly what is incountryotherwise theIfstatement will not work (especially since it is logically combined with anAND). Hence, you will (should) always get theno such countryerror message.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).Rowcountrygets the value ofSheets("Results").Range("D5").ValueIf this cell containsUSAthen your lineElseIf Sheets("Database").Cells(i, 1) = country And _comparesSheets("Database").Cells(i, 1)toUSA. If they are equal then yourElseIfcan work (if the otherifcases are met). Yet, onlyUSAwill be accepted. The following will not work:_USA,uSA,USA,usa, etc. Also, onlyUSAis accepted (no other country).