Excel VBA中的IF ElseIF语句
我正在尝试为基于国家的数据库搜索编写脚本。搜索在一张名为“数据库”的数据表上运行,结果粘贴到另一张名为“结果”的数据表上 搜索取决于用户输入的变量,我将其定义为“国家”、“类别”和“子类别”: 我希望用户通过Excel VBA中的IF ElseIF语句,vba,excel,if-statement,Vba,Excel,If Statement,我正在尝试为基于国家的数据库搜索编写脚本。搜索在一张名为“数据库”的数据表上运行,结果粘贴到另一张名为“结果”的数据表上 搜索取决于用户输入的变量,我将其定义为“国家”、“类别”和“子类别”: 我希望用户通过UserForm填写的搜索条件能够导致不同的场景。因此: 1-如果用户搜索的国家不包括在数据库中,搜索将不会运行,并会弹出一条消息,说明这样做。我使用了.Find函数来执行以下操作: With Worksheets("Database") Set c = .Range("A:
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-作为最后一个场景,我希望脚本对用户提供“国家”、“类别”和/或“子类别”的事件作出反应,但是,尽管数据库中有该国家的条目,但在该类别或子类别中没有该国家的条目。在本例中,我希望脚本询问用户是否希望查看搜索国家的所有信息,而不考虑“类别”或“子类别”。我试图通过前面的IF
语句中的IfElse
语句来实现这一点:
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
专用子搜索按钮\u单击()
Dim country作为字符串的搜索查询类别用户输入
Dim类别作为字符串“搜索查询类别用户输入”
Dim子类别作为字符串的搜索查询类别用户输入
将finalrow设置为整数
Dim LastRowForTable As Long“结果表中最后填充的行”
Dim i作为整数行计数器
将ws设置为工作表
设置ws=Sheets(“数据库”)
'从结果表中删除所有条目
表格(“结果”)。范围(“B10:J200000”)。ClearContent
'定义用户输入的变量
国家=表格(“结果”).范围(“D5”).值
类别=表格(“结果”).范围(“D6”).值
子类别=工作表(“结果”).范围(“D7”).值
finalrow=工作表(“数据库”)。范围(“A200000”)。结束(xlUp)。行
'用于搜索的If语句
使用工作表(“数据库”)
设置c=.Range(“A:A”).Find(What:=国家,After:=.Cells(1,1)_
LookIn:=xlValues,LookAt:=xlPart,SearchOrder:=xlByRows_
SearchDirection:=xlNext,MatchCase:=False)
如果不是,那么c什么都不是
其他的
MsgBox“很遗憾,数据库在“&country&”中没有任何信息源。请在中搜索与其他国家/地区相关的信息源。”
表格(“结果”)。范围(“D5”)。清晰内容
表格(“结果”)。范围(“D6”)。清晰内容
表格(“结果”)。范围(“D7”)。清晰内容
出口接头
如果结束
以
“如果国家/地区字段为空,甚至不需要进行搜索
如果国家=“那么
表(“结果”)。范围(“B10:J200000”)。清除
MsgBox“您必须选择一个国家才能搜索数据库。请在提供的下拉列表中选择。”
表格(“结果”)。范围(“D5”)。清晰内容
表格(“结果”)。范围(“D6”)。清晰内容
表格(“结果”)。范围(“D7”)。清晰内容
出口接头
如果结束
对于i=2到最后一行
'如果填写了国家/地区字段,并且有搜索结果
如果表格(“数据库”)。单元格(i,1)=国家和地区_
(表(“数据库”)。单元格(i,3)=类别或类别=”)和_
(表(“数据库”)。单元格(i,4)=子类别或子类别=”)然后
'复制“数据库”工作表的标题
带工作表(“数据库”)
.范围(“A1:I1”).副本
以
表格(“结果”)。范围(“B10:J10”)。特殊粘贴
'复制表中与搜索查询匹配的行
带工作表(“数据库”)
.Range(.Cells(i,1),.Cells(i,9)).Copy
以
表格(“结果”)。范围(“B20000”)。结束(xlUp)。偏移量(1,0)。粘贴特殊XLPaste公式和数字格式
其他表格(“数据库”)。单元格(i,1)=国家和地区_
(表格(“数据库”)。单元格(i,3)类别)然后
作为整数的模糊问题
QUOTE=MsgBox(“不幸的是,数据库在“&country&”中没有关于“&Category&”的源代码。您是否希望扩大搜索范围并查看所有关于“&country&”的源代码,vbYesNo+VBQUOTE,“空表”)
MsgBox问题
如果问题=是,那么
表格(“结果”)。范围(“D6”)。清晰内容
表格(“结果”)。范围(“D7”)。清晰内容
类别=表格(“结果”).范围(“D6”).值
子类别=工作表(“结果”).范围(“D7”).值
如果表格(“数据库”)。单元格(i,1)=国家和地区_
(表(“数据库”)。单元格(i,3)=类别或类别=”)和_
(表(“数据库”)。单元格(i,4)=子类别或子类别=”)然后
'复制“数据库”工作表的标题
带工作表(“数据库”)
.范围(“A1:I1”).副本
以
表格(“结果”)。范围(“B10:J10”)。特殊粘贴
'复制与搜索查询匹配的“数据库”行
带工作表(“数据库”)
.Range(.Cells(i,1),.Cells(i,9)).Copy
以
表格(“结果”)。范围(“B20000”)。结束(xlUp)。偏移量(1,0)。粘贴特殊XLPaste公式和数字格式
如果结束
其他的
表格(“结果”)。范围(“D5”)。清晰内容
表格(“结果”)。范围(“D6”)。清晰内容
表格(“结果”)。范围(“D7”)。清晰内容
出口苏
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
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
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")
MsgBox question
If question = vbYes Then
Sheets("Results").Range("D6").ClearContents
Sheets("Results").Range("D7").ClearContents
Category = Sheets("Results").Range("D6").Value
Subcategory = Sheets("Results").Range("D7").Value
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
End If
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