I have used the following code (from OzGrid Website) to convert selected text to UPPER case, which works just fine.
This conversion to UPPER text will be applied to a customer's name cell (containing both Surname and Forename), ie STANLEY PAUL
When the conversion has been done I would also like the Macro to insert a comma and a space in between the Surname and Forename ie STANLEY, PAUL This would be handy if the cells were ever split into separate Forename and Surname cells.
Regards
Paul S
This conversion to UPPER text will be applied to a customer's name cell (containing both Surname and Forename), ie STANLEY PAUL
When the conversion has been done I would also like the Macro to insert a comma and a space in between the Surname and Forename ie STANLEY, PAUL This would be handy if the cells were ever split into separate Forename and Surname cells.
Code:
Sub ConvertCase()
Dim rAcells As Range, rLoopCells As Range
Dim lReply As Long
'Set variable to needed cells
If Selection.Cells.Count = 1 Then
Set rAcells = ActiveSheet.UsedRange
Else
Set rAcells = Selection
End If
On Error Resume Next 'In case of NO text constants.
'Set variable to all text constants
Set rAcells = rAcells.SpecialCells(xlCellTypeConstants, xlTextValues)
If rAcells Is Nothing Then
MsgBox "Could not find any text."
On Error GoTo 0
Exit Sub
End If
lReply = MsgBox("Select 'Yes' for UPPER CASE or 'No' for Proper Case.", vbYesNoCancel, "")
If lReply = vbCancel Then Exit Sub
If lReply = vbYes Then ' Convert to Upper Case
For Each rLoopCells In rAcells
rLoopCells = StrConv(rLoopCells, vbUpperCase)
Next rLoopCells
Else ' Convert to Proper Case
For Each rLoopCells In rAcells
rLoopCells = StrConv(rLoopCells, vbProperCase)
Next rLoopCells
End If
End Sub
Regards
Paul S