[VBA] How to Display Input Guides with Popups: Mastering InputMessage

目次

Introduction

Excel has a “Data Validation” feature that allows you to specify rules for values entered into cells. You can also use this feature to display a popup hint to the user when they select a cell.

In this article, I will introduce how to use VBA to display these input guide messages with practical sample code.

Benefits of Using Validation with Popups

  • Clarity: Users can understand the required format and conditions before typing.
  • Accuracy: It maintains data integrity and saves time on corrections later.
  • Quality: It improves the usability and professionalism of forms and business templates.

VBA Code: Displaying an Input Guide

The following code sets a rule for Column D to accept “dates on or after January 1, 2025” and displays a popup message explaining this when the mouse selects the cell.

Sub SetInputGuide()

    With Worksheets("Sheet1").Range("D:D").Validation
        ' Delete existing validation rules
        .Delete

        ' Add new validation rule (Date: 2025/1/1 or later)
        .Add _
            Type:=xlValidateDate, _
            Operator:=xlGreaterEqual, _
            Formula1:="2025/1/1"

        ' Popup message displayed upon selection
        .InputTitle = "Date Input Instructions"
        .InputMessage = "Please enter a date on or after Jan 1, 2025."
    End With

End Sub

Explanation of Properties

Property NameDescription
.DeleteDeletes any existing validation rules.
.AddAdds a new validation rule.
TypeSpecifies the input type (e.g., xlValidateDate for dates).
OperatorSpecifies the condition (e.g., xlGreaterEqual for “greater than or equal to”).
Formula1Specifies the condition value (e.g., "2025/1/1").
.InputTitleThe title of the popup box.
.InputMessageThe explanatory text displayed in the popup.

Examples of Use Cases

SituationConditionPopup Message
Shipping Date FormFuture dates only“Please enter a date from today onwards.”
Customer ListSelect from dropdown only“Please select a customer name from the list.”
Amount FieldRange 1 to 10,000“Enter an amount between 1 and 10,000.”

Important Notes

  • The popup defined by .InputMessage is displayed only when the cell is selected.
  • The message disappears automatically when the user moves to another cell.
  • The restriction set by .Add remains active regardless of whether the message is shown.

Summary

  • Set Rules: Use .Validation.Add.
  • Show Hints: Set .InputTitle and .InputMessage.
  • Common Types: Use xlValidateDate, xlGreaterEqual, etc.
  • Benefit: Provides input assistance to users and improves data accuracy.

By combining validation rules with hint messages using VBA, you can build Excel sheets that are easy to understand and highly reliable. Please try using this in your projects.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次