Force Text to Upper Case or Proper Case.
We can use Excel VBA code in the Private Module of the Worksheet Object to force any text entered to be UPPER case, or Proper case. To use either of the first 4 Excel VBA procedures below, right click on the Worksheet name tab, select View Code and in here paste one and one only of the 4 procedures directly below. Note that 2 of the procedures restricts the forcing of upper/proper case to a specified range on the Worksheet. The other 2 similar procedures will do the same but for the entire Worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
''''''''''''''''''''''''''''''''''''''''''''
'Forces text to UPPER case for the range A1:B20
''''''''''''''''''''''''''''''''''''''''''''
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("A1:B20")) Is Nothing Then
Application.EnableEvents = False
Target = UCase(Target)
Application.EnableEvents = True
End If
On Error GoTo 0
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
''''''''''''''''''''''''''''''''''''''''''''
'Forces all text to UPPER case
''''''''''''''''''''''''''''''''''''''''''''
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Target = UCase(Target)
Application.EnableEvents = True
On Error GoTo 0
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
''''''''''''''''''''''''''''''''''''''''''''
'Forces text to Proper case for the range A1:B20
''''''''''''''''''''''''''''''''''''''''''''
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("A1:B20")) Is Nothing Then
Application.EnableEvents = False
Target = StrConv(Target, vbProperCase)
Application.EnableEvents = True
End If
On Error GoTo 0
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
''''''''''''''''''''''''''''''''''''''''''''
'Forces all text to Proper case
''''''''''''''''''''''''''''''''''''''''''''
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Target = StrConv(Target, vbProperCase)
Application.EnableEvents = True
On Error GoTo 0
End Sub
Force Text to Upper Case/Proper Case TextBox ControlThe excel VBA codes below can be used in an ActiveX TextBox control to force UPPER or Proper case.
Private Sub TextBox1_Change()
On Error Resume Next
TextBox1 = UCase(TextBox1)
On Error GoTo 0
End Sub
Private Sub TextBox1_Change()
On Error Resume Next
TextBox1 = StrConv(TextBox1, vbProperCase)
On Error GoTo 0
End Sub
See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.