Unfortunately, no one has found a way to zap users with a mild shock from the keyboard, so we have to rely on messages to help people do the right things in Excel.
不幸的是,没有人找到一种使键盘受到轻微震动的方法,因此我们必须依靠消息来帮助人们在Excel中做正确的事情。
You can create messages without Excel VBA. For example, you can add a comment to a cell,
您可以在没有Excel VBA的情况下创建消息。 例如,您可以在单元格中添加评论 ,
or use a data validation input message.
或使用数据验证输入消息 。
To keep things really simple, you could even type a message in a cell, or in a textbox on the worksheet.
为了使事情变得非常简单,您甚至可以在单元格或工作表上的文本框中键入一条消息。
使用Excel VBA创建消息 (Create a Message With Excel VBA)
Sometimes you need a message with more impact – visual impact, that is. Maybe the users are ignoring your polite and informative worksheet messages, and you want a message that's hard to miss.
有时,您需要一条具有更大影响力的消息-即视觉冲击力。 也许用户忽略了您的礼貌而翔实的工作表消息,而您想要的一条消息很难错过。
You can use Excel VBA to create a message box, and make that message appear when someone tries to print the order form.
您可以使用Excel VBA创建一个消息框,并在有人尝试打印订单时使该消息出现。
创建宏 (Create the Macro)
You can't record all the steps for creating a message, but you can use the Record Macro button to get started.
您不能记录创建消息的所有步骤,但是可以使用“记录宏”按钮开始使用。
- At the bottom left of the Excel 2007 window, click Record Macro 在Excel 2007窗口的左下方,单击“记录宏”
- Type a one word name for the macro, e.g. CustomerMessage 为宏键入一个单词名称,例如CustomerMessage
- Store the macro in 'This Workbook' and click OK 将宏存储在“此工作簿”中,然后单击“确定”。
- At the bottom left of the Excel 2007 window, click Stop Recording 在Excel 2007窗口的左下方,单击“停止记录”
To see the macro code that you created:
要查看您创建的宏代码:
- On the Excel Ribbon, click the View tab, then click Macros 在Excel功能区上,单击“视图”选项卡,然后单击“宏”
-
- Click on your macro's name, then click Edit 单击您的宏的名称,然后单击编辑
The Visual Basic Editor (VBE) opens, and you can see the macro. There are Sub and End Sub lines, and one comment line.
将打开Visual Basic编辑器(VBE),您可以看到该宏。 有Sub和End Sub行,以及一条注释行。
输入一行代码 (Type a Line of Code)
You can't record the code that shows the message box, so you'll have to type it.
您无法记录显示消息框的代码,因此必须输入它。
- Click in the blank line between the Sub and End Sub lines. 单击“子”行和“结束子”行之间的空白行。
Type this code: Msgbox "Select a Customer Name"
键入以下代码: Msgbox“选择客户名称”
测试宏 (Test the Macro)
To see warning message, switch back to Excel.
要查看警告消息,请切换回Excel。
- On the Excel Ribbon, click the View tab, then click Macros 在Excel功能区上,单击“视图”选项卡,然后单击“宏”
- Click View Macros 单击查看宏
- Click on your macro's name, then click Run 单击您的宏的名称,然后单击运行
The message will appear in the centre of the Excel window.
该消息将出现在Excel窗口的中心。
- Click OK to close the message. 单击确定关闭消息。
编辑消息框宏 (Edit the Message Box Macro)
The message box appears, and the user will have to click OK before they can continue working in Excel. That should get their attention, but you can add an icon to the message box, to make it look scarier.
出现消息框,用户必须单击“确定”才能继续在Excel中工作。 这应该引起他们的注意,但是您可以在消息框中添加一个图标,以使其看起来更吓人。
- Go back to the VBE, to edit your macro. 回到VBE,以编辑您的宏。
- Click at the end of the MsgBox line, and type a comma 单击MsgBox行的末尾,然后键入一个逗号
-
- Use the down arrow on your keyboard to select vbCritical 使用键盘上的向下箭头选择vbCritical
- Press the Enter key to select that option 按Enter键选择该选项
Test your macro again, to see the Critical icon in the revised message box.
再次测试您的宏,以查看修改后的消息框中的“严重”图标。
Woohoo! That should keep those pesky users in line.
hoo! 那应该使那些讨厌的用户保持一致。
使宏变得更好 (Make the Macro Even Better)
Next week, we'll revise the macro again, so it only runs if there's no customer name in cell B5. Then, we'll make the macro run automatically, if someone tries to print the worksheet, without a customer name selected. _________________ Previous Excel VBA articles:
下周,我们将再次修改该宏,因此仅当单元格B5中没有客户名称时,该宏才运行。 然后,如果有人尝试打印工作表而未选择客户名称,我们将使宏自动运行。 _________________以前的Excel VBA文章:
Getting Started With Excel VBA
Excel VBA入门
Excel VBA: Edit Your Recorded Macro
Excel VBA:编辑记录的宏
Excel VBA: Switch Column Headings to Numbers
Excel VBA:将列标题切换为数字
翻译自: https://contexturesblog.com/archives/2009/12/11/excel-vba-show-a-message-to-users/