andy-h
3/22/2017 - 6:14 PM

Excel macros for simple stuff I do often, including changing text casing and number formatting.

Excel macros for simple stuff I do often, including changing text casing and number formatting.

Sub ToLowerCase()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.Value = LCase(cell.Value)
    Next
    
End Sub

Sub ToUpperCase()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.Value = UCase(cell.Value)
    Next
    
End Sub

Sub ConvertToText()

    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "@"
        cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
    Next
    
End Sub

Sub ConvertToGeneral()

    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "General"
        cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
    Next
    
End Sub

'update cell values to match the number format of their cell
'if there are formulas in the selection, the user will be asked whether to convert them to values or not
Sub ForceNumberFormat()
    
    Dim cell As Object, answer
    
    answer = vbYes
    If Selection.HasFormula Then
        answer = MsgBox("Convert formulas to values?", vbYesNoCancel)
        If answer = vbCancel Then
            Exit Sub
        End If
    End If
    
    For Each cell In Selection
        If Not (cell.HasFormula And answer = vbNo) Then
            cell.FormulaR1C1 = CStr(cell.Value) 'does the same thing as pressing F2 and Enter
        End If
    Next
    
End Sub

Sub PercentToInteger()
    
    Dim cell As Object
    
    For Each cell In Selection
        cell.NumberFormat = "0"
        If IsNumeric(cell) And Not IsEmpty(cell) Then
            cell.Value = cell.Value * 100
        End If
    Next
    
End Sub