VBA Macro to Count Characters in a cell

Larcen60

New Member
Joined
Sep 12, 2017
Messages
18
Hello!

I'm trying to create a macro that gives a message box and fills a cell red when a user enters over 15 characters in a cell. However, when I create the macro I notice that it only runs if you click back into the cell a second time. Is there any way to make the macro run upon exiting the cell the first time? My concern is that a user won't click back into the cell once they've already entered too many characters. Also, is there a way to make the red fill go away upon the user correcting the cell to 15 characters or less?

Thanks very much in advance!


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
     If Len(Target) > 15 Then
     MsgBox "Text is over 15 characters."
     Range("A1").Interior.Color = RGB(255, 0, 0)
          End If
End If
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
    If Len(Range("A1")) > 15 Then
        MsgBox "Text is over 15 characters."
        Range("A1").Interior.Color = RGB(255, 0, 0)
        Range("A1").Select
    Else
        Range("A1").Interior.Color = xlNone
    End If
End If
End Sub
 
Upvote 0
Any reason to use a macro instead of Excel's standard Data Validation (on the Data Ribbon tab) with the option Allow: Text length?
 
Upvote 0
That's perfect, thanks so much! Worked great!

Peter, I didn't use Data Validation because I was hoping to get a cell fill in there as well. I don't think Data Validation will allow a cell fill, although I might be wrong!

Thanks again for the help!
 
Upvote 0
Conditional Formatting will do it too...or maybe along with validation...

Use formula as rule (or however it words it :) )

=LEN(A1)>15
 
Upvote 0
Peter, I didn't use Data Validation because I was hoping to get a cell fill in there as well. I don't think Data Validation will allow a cell fill,
You are right that DV does not offer that feature. However, with DV the feature should not be needed as the the DV can stop the user entering more than 15 characters in the first place! :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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