Validate Character Limits Using Visual Basic

Your rating: None Average: 2 (1 vote)

This is a VB excel macro code to prevent users from entering data that exceeds the field limit.

For example, if a cell needs to be only 5 characters long and user entered 7 characters, an error message will popup notifying the user and highlighting the cell containing the 7 characters.

ActiveSheet.Range("A" + posrow).Activate
If Len(ActiveCell.Value) > 5 Then
   Application.ScreenUpdating = True
   ActiveSheet.Range("A" + posrow).Activate
   MsgBox "This Cell Can Not Exceed 5 Characters.", 0, "Data Error."
End If

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!