Search Contextures Sites ![]()
Excel Data Validation -- Display Input Messages in a Text Box
Set up the Workbook
Create a Data Validation Dropdown List
Add the Text Box
Name the Text Box
Add the Code
Test the CodeDownload the zipped sample file
You can use a Data Validation Input Message to display a message when a cell is selected. However, the font can't be changed, nor can message box size be controlled.To overcome these limitations, you can create a text box to display the message, and use programming to make it appear if cells that contain a data validation Input Message are selected.
Set up the WorkbookTwo worksheets are required in this workbook.
- Delete all sheets except Sheet1 and Sheet2
- Rename Sheet1 as ValidationSample
- Rename Sheet2 as ValidationLists
On the ValidationLists sheet, type the lists that will be used in the data validation dropdowns:
Tip: Use the AutoFill feature to create the lists
- In cells A1:A7 type a list of weekdays
- In cells C1:C12 type a list of months
Name the lists (there are Naming instructions here: Name a Range):
Create a Data Validation Dropdown ListThe next step is to create the data validation dropdown lists. There are detailed instructions here: Data Validation -- Introduction
- Cells C5:C15 have data validation lists with the source DayList. When a cell in this range is selected, a dropdown list of weekdays is available. Include an Input Message, as described here: Display Messages to the User . The message used in the sample file is:
Title: Activation Day
Message: Please select the weekday in which the product was originally purchased, not the weekday in which it was first used. If you are not sure, please leave this cell blank, then check with your manager, and fill in the weekday later.- Cells D5:D15 have data validation lists with the source MonthList. When a cell in this range is selected, a dropdown list of months is available. Include an Input Message. The message used in the sample file is:
Title: Activation Month
Message: Please select the month in which the product was originally purchased, not the month in which it was first used. If you are not sure, please leave this cell blank, then check with your manager, and fill in the month later.![]()
![]()
Name the Text box
Visual Basic for Applications (VBA) code is required to make the text box appear when you select a cell that contains a data validation input message. It copies the data validation Input Message and Input Title to the text box, and makes the title bold.
Copy the following code:
'========================================= Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim strTitle As String Dim strMsg As String Dim lDVType As Long Dim sTemp As Shape Dim ws As Worksheet Application.EnableEvents = False Set ws = ActiveSheet Set sTemp = ws.Shapes("txtInputMsg") On Error Resume Next lDVType = 0 lDVType = Target.Validation.Type On Error GoTo errHandler If lDVType = 0 Then sTemp.TextFrame.Characters.Text = "" sTemp.Visible = msoFalse Else If Target.Validation.InputTitle <> "" Or _ Target.Validation.InputMessage <> "" Then strTitle = Target.Validation.InputTitle & Chr(10) strMsg = Target.Validation.InputMessage With sTemp.TextFrame .Characters.Text = strTitle & strMsg .Characters.Font.Bold = False .Characters(1, Len(strTitle)).Font.Bold = True End With sTemp.Visible = msoTrue Else sTemp.TextFrame.Characters.Text = "" sTemp.Visible = msoFalse End If End If errHandler: Application.EnableEvents = True Exit Sub End Sub '====================================
To add this code to the worksheet:
Test the Code
- Select one of the cells that contains a data validation input message.
- The text box will appear
- Select a cell that doesn't contain a data validation in put message
- The text box disappears.
Download the zipped sample file
Excel Tutorials - Data Validation
Contextures Inc., Copyright ©2010
All rights reserved.