Tuesday, July 5, 2011

Converting Text Case in Excel VBA :-



If You want to Convert Text Case in Selection :-

Sub ConvertProperCase()
Application.ScreenUpdating = False
    Dim Rng As Range
    For Each Rng In Selection.Cells
        If Rng.HasFormula = False Then
             'Use this line for ProperCase text; change UCase to LCase for LowerCase text.
            Rng.value = Application.Proper(Rng.value)
        End If
    Next Rng
End Sub

Sub ConvertLowerCase()
Application.ScreenUpdating = False
    Dim Rng As Range
    For Each Rng In Selection.Cells
        If Rng.HasFormula = False Then
             'Use this line for LowerCase text; change UCase to LCase for LowerCase text.
            Rng.value = LCase(Rng.value)
        End If
    Next Rng
End Sub

Sub ConvertUpperCase()
Application.ScreenUpdating = False
    Dim Rng As Range
    For Each Rng In Selection.Cells
        If Rng.HasFormula = False Then
             'Use this line for UpperCase text; change UCase to LCase for LowerCase text.
            Rng.value = UCase(Rng.value)
        End If
    Next Rng
End Sub

2 comments:

siti Vi said...

About Text Conversion to Proper Case

Instead of using WorksheetFunction / Application function :
Rng.value = Application.Proper(Rng.value)

We can use VBA function:

strX = StrConv("AAA BBB CCC DDD", vbProperCase)

resultng strX = "Aaa Bbb Ccc Ddd"

EXAMPLE

Sub ConvertProperCase()
Application.ScreenUpdating = False
Dim Cell As Range
For Each Cell In Selection.Cells
If Not Cell.HasFormula Then
Cell.Value = StrConv(Rng, vbProperCase)
End If
Next Cell
Application.ScreenUpdating = False
End Sub

siti Vi said...
This comment has been removed by the author.