带有用户inputvariables的MsgBox

我正在Excel VBA中编写一个脚本,该脚本根据用户input的标准在国家数据库中运行search。 search从用户UserForm用尽了三个search字段的用户search。 除了“国家”之外,用户还可以通过提及“信息类别”和“感兴趣的信息子类别”缩小search范围,所有这些字段都是链接到列表的ComboBox 。 类别和子类别的一些例子是“地理”,“经济指标”,“媒体”,“人口统计”等等。 根据用户提供的标准,脚本将返回search结果 – 如果与数据库有任何匹配 – 或者MsgBox通知search未find匹配项。 我想知道MsgBox提供的文本是固定的还是可以依赖于用户input的variables。

为了说明一下,我们举个例子,一个正在寻找美国信息的用户,只用这个标准来进行search。 该数据库包含有关美国的信息,并返回所有可用的信息。 尽pipe所有的数据,用户特别想要在美国有关媒体的信息,并重复search这两个标准。 但是,数据库没有具体的信息在美国和媒体。 在这种情况下,脚本返回一个MsgBox ,根据我目前的代码 – 哪个工作正常 – 只是说“数据库没有匹配这个search的信息”。

我的问题是: MsgBox是否可以返回一个依赖于用户search的消息,例如,返回类似“没有关于美国媒体的信息”的东西? 非常感谢您的帮助。

这是运行search的代码:

 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 For i = 2 To finalrow 'If the country field is left empty 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 country field is filled in and there results from the search made 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 'Copy the headers of the table 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 MsgBox "The database has no information that matches this search." Sheets("Results").Range("D5").ClearContents Sheets("Results").Range("D6").ClearContents Sheets("Results").Range("D7").ClearContents Exit Sub End If Next i 

您正在控制MsgBox所以您只需正确处理您的逻辑即可相应地更改消息。

一个非常简单的方法就是简单地改变这一行:

 MsgBox "The database has no information that matches this search." 

用你的variables的值,就像你提到的那样:

 MsgBox "There is no information regarding " & Category & " in the " & country & "." 

假设Category包含“媒体”,而country在search时包含“美国”,则会输出“没有关于美国媒体的信息”。 如你所料。

如果您要根据用户input的确切值设置不同的消息模式,则必须使用更多的If...Then...Else才能正确处理它们。 在这里,我只是使用一个设定的短语,并用参数改变硬编码的值。