Skip to content

Instantly share code, notes, and snippets.

@wizard04wsu
Last active March 22, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wizard04wsu/7f35a60614e3af4e1c78eefe1787e954 to your computer and use it in GitHub Desktop.
Save wizard04wsu/7f35a60614e3af4e1c78eefe1787e954 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment