Can you use variables in a data validation error alert message?

evaprotrans

New Member
Joined
Mar 3, 2015
Messages
6
I have cells with character limits that use data validation. I would like the error alert message to say something like "You have entered X characters; only 200 are allowed", to give the user an idea how much they need to shorten their text. Is it possible to insert a variable (current length of the cell) into the error message?

Thanks,
Eva
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I dont think it is possible to use Excel data validation for that. You could do it through the use of VBA and a worksheet change event.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    
    If Target = Range("H5") Then
        Call CheckLength(Target, 25)
    End If
End Sub


Function CheckLength(rng As Range, maxLength As Integer) As Boolean
    Dim val As String: val = rng.Value
    Dim valLength As Integer: valLength = Len(val)
    If valLength > maxLength Then
        MsgBox "You have entered " & valLength & " characters. " & vbCrLf & vbCrLf & "Only " & maxLength & " are allowed.", vbCritical
    End If
End Function
 
Upvote 0
I know this is an old thread but I was looking for a similar issue and noticed an error in this code. The code as written will compare the value of Target to the value in H5; it doesn't determine if Target is the same cell as H5.
Code:
    If Target.Address = Range("H5").Address Then
Even better, this version deals correctly with the situation when more than one cell is changed at a time:
Code:
    If Not Intersect(Target, Range("H5") Is Nothing Then
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top