✅ HTML Code: VBA Case Conversion Macros for MS Word & Excel

VBA Code for Case Conversion in Word & Excel

💻 VBA Case Conversion Macros for MS Word and MS Excel

This page includes useful VBA macros to convert selected text to UPPER CASE, lower case, Proper Case, and Toggle Case for both Microsoft Word and Excel.

📄 Microsoft Word VBA Codes

🔠 Convert to UPPER CASE

Sub ConvertToUpperCase()
    Selection.Range.Text = UCase(Selection.Range.Text)
End Sub

🔡 Convert to lower case

Sub ConvertToLowerCase()
    Selection.Range.Text = LCase(Selection.Range.Text)
End Sub

🅿️ Convert to Proper Case

Sub ConvertToProperCase()
    Selection.Range.Text = StrConv(Selection.Range.Text, vbProperCase)
End Sub

🔁 Toggle Between All Cases

Sub ToggleTextCase()
    Dim txt As String
    Dim newText As String

    txt = Selection.Range.Text

    If txt = UCase(txt) Then
        newText = StrConv(txt, vbProperCase)
    ElseIf txt = StrConv(txt, vbProperCase) Then
        newText = LCase(txt)
    Else
        newText = UCase(txt)
    End If

    Selection.Range.Text = newText
End Sub

📊 Microsoft Excel VBA Codes

🔠 Convert to UPPER CASE (Selected Cells)

Sub ConvertRangeToUpper()
    Dim cell As Range
    For Each cell In Selection
        If Not IsEmpty(cell.Value) Then
            cell.Value = UCase(cell.Value)
        End If
    Next cell
End Sub

🔡 Convert to lower case (Selected Cells)

Sub ConvertRangeToLower()
    Dim cell As Range
    For Each cell In Selection
        If Not IsEmpty(cell.Value) Then
            cell.Value = LCase(cell.Value)
        End If
    Next cell
End Sub

🅿️ Convert to Proper Case (Selected Cells)

Sub ConvertRangeToProper()
    Dim cell As Range
    For Each cell In Selection
        If Not IsEmpty(cell.Value) Then
            cell.Value = Application.WorksheetFunction.Proper(cell.Value)
        End If
    Next cell
End Sub

🔡 Convert to L P U CASE

 Sub ChangeTextCase()
    Dim rng As Range
    Dim cell As Range
    Dim userChoice As String

    ' Ask user what case to apply
    userChoice = InputBox("Enter your choice: U for UPPERCASE, L for lowercase, P for Proper Case")

    ' Exit if cancelled
    If userChoice = "" Then Exit Sub

    ' Apply to selected range or entire sheet if nothing is selected
    If TypeName(Selection) = "Range" Then
        Set rng = Selection
    Else
        Set rng = ActiveSheet.UsedRange
    End If

    ' Loop through each cell and change case
    For Each cell In rng
        If Not IsEmpty(cell.Value) And VarType(cell.Value) = vbString Then
            Select Case UCase(userChoice)
                Case "U"
                    cell.Value = UCase(cell.Value)
                Case "L"
                    cell.Value = LCase(cell.Value)
                Case "P"
                    cell.Value = WorksheetFunction.Proper(cell.Value)
                Case Else
                    MsgBox "Invalid choice! Please enter U, L, or P.", vbExclamation
                    Exit Sub
            End Select
        End If
    Next cell

    MsgBox "Text case updated!"
End Sub 

Comments

Popular posts from this blog

Site password set pest kro Head ke uper