You can use Excel VBA to show messages to someone who's using your workbook. A message can be simple, with just an OK button, or give people options, with Yes and No buttons. That lets you collect data from users, and then complete macro steps based on which button they clicked.
您可以使用Excel VBA向正在使用您的工作簿的人显示消息 。 消息可以很简单,只需单击“确定”按钮,或使用“是”和“否”按钮给人们提供选项。 这样,您就可以从用户那里收集数据,然后根据用户单击的按钮完成宏步骤。
带有“确定”按钮的简单消息 (Simple Message with OK Button)
This simple message lets the user know that a customer name must be selected, and there's an OK button to click after reading the message. (No one would ever click the OK button without reading the message, would they?)
这个简单的消息使用户知道必须选择一个客户名称,并且在阅读消息后有一个确定按钮可以单击。 (没有人会在不阅读消息的情况下单击“确定”按钮,对吗?)
带有“是”和“否”按钮的消息 (Message With Yes and No Buttons)
But sometimes you want to get information from a user, instead of giving information. This video shows how to create a message with Yes and No buttons, and then run macro steps based on which button is clicked.
但是有时您想要从用户那里获取信息,而不是提供信息。 该视频显示了如何使用“是”和“否”按钮创建消息,然后根据单击的按钮运行宏步骤。
There are written steps below the video.
视频下方有书面步骤。
演示地址
将按钮添加到Excel消息框中 (Add Buttons to an Excel Message Box)
The message box that's shown above appears if someone tries to print an order form, and a customer name hasn't been selected. Here's the code that we created in the previous blog post on using Excel VBA to show messages.
如果有人尝试打印订单并且未选择客户名称,则会出现上面显示的消息框。 这是我们在上一篇博客文章中使用Excel VBA显示消息的代码 。
A message box can return information to Excel VBA, as well as give information to the user. Instead of a single OK button on the message box, you could show Yes and No buttons. Perhaps the user needs to print an order form occasionally, without a customer name.
一个消息框可以将信息返回给Excel VBA,也可以向用户提供信息。 除了显示消息框上的单个“确定”按钮外,您还可以显示“是”和“否”按钮。 也许用户偶尔需要打印一个没有客户名称的订单。
We'll change the message text, to ask, "Print without Customer Name?" and we'll change the message box style from vbCritical to vbYesNo.
我们将更改消息文本,询问“不使用客户名称打印?”。 并将消息框样式从vbCritical更改为vbYesNo。
Now, if you try to print with no customer name, the message box shows the Yes and No buttons, with the new text.
现在,如果您尝试不使用客户名称进行打印,则消息框中将显示“是”和“否”按钮以及新的文本。
更改按钮行为 (Change the Button Behaviour)
With the new message box code, we have two buttons, but it doesn't matter which one you click – the printing is still cancelled. We'd like to change the code so that if you click No, the printing is cancelled, and if you click Yes, the order form is printed.
使用新的消息框代码,我们有两个按钮,但是单击哪个按钮都没关系–打印仍被取消。 我们想更改代码,以便如果您单击“否”,则取消打印,如果您单击“是”,则打印订单。
When someone clicks a button in a message box, it returns a numeric value. There's a list in the Excel VBA Help that shows the value returned by each button.
当某人单击消息框中的按钮时,它将返回一个数值。 Excel VBA帮助中有一个列表,显示每个按钮返回的值。
So, if someone clicks the Yes button, it returns a numeric value of 6. We'll change the code to capture this.
因此,如果有人单击“是”按钮,它将返回数字值6。我们将更改代码以捕获该值。
- We'll add a variable, lRsp, to the code, to store the returned value. 我们将在代码中添加一个变量lRsp,以存储返回的值。
- At the start of the MsgBox line, add the lRsp variable, and enclose the MsgBox arguments in brackets. 在MsgBox行的开头,添加lRsp变量,并将MsgBox参数括在方括号中。
- Add an If...End If statement, to cancel the printing if the returned value is not equal to 6. 添加If ... End If语句,以在返回值不等于6时取消打印。
下载样本文件 (Download the Sample File)
To see the order form and the completed Excel VBA code, you can download the Excel order form. Enable macros when the file opens.
要查看订单和完整的Excel VBA代码,可以下载Excel订单 。 文件打开时启用宏。
使用Excel用户窗体 (Use an Excel UserForm)
If you need to collect more than a Yes or No response from your users, you can use an Excel UserForm.
如果您需要收集用户的“是”或“否”答复,则可以使用Excel UserForm。
There are written instructions and video tutorials here: Excel UserForm With ComboBoxes _______________
这里有书面说明和视频教程: 带组合框的Excel UserForm _______________
翻译自: https://contexturesblog.com/archives/2010/02/26/collect-data-from-users-in-excel-vba/