Pages

Wednesday, October 19, 2011

Add or Remove apostrophe to cell in active excel sheet

Option Explicit
Sub DeathToApostrophe()
    Dim s As Range, temp As String
    If MsgBox("Are you sure you want to remove all leading apostrophes from the entire sheet?", _
    vbOKCancel + vbQuestion, "Remove Apostrophes") = vbCancel Then Exit Sub
    Application.ScreenUpdating = False
    For Each s In ActiveSheet.UsedRange
        If s.HasFormula = False Then
             'Gets text and rewrites to same cell without   the apostrophe.
            s.Value = s.Text
        End If
    Next s
    Application.ScreenUpdating = True
End Sub


Sub AddApostrophes()
    Dim cel As Range
    Dim strw As String
    
    strw = "You cannot undo this task."
    strw = strw & " You may want to save your work to a new file."
    strw = strw & " Press OK to proceed, Cancel to quit."
    If vbCancel = MsgBox(strw, vbOKCancel + vbCritical, "Task Cannot Be Undone") Then Exit Sub
    
    For Each cel In ActiveSheet.UsedRange
        cel.Value = "'" & cel.Value
    Next cel
End Sub


How to use
  1. Copy above code.
  2. In Excel press Alt + F11 to enter the VBE.
  3. Right-click desired file on left.
  4. Choose Insert -Module.
  5. Paste code into the right pane.
  6. Press Alt + Q to close the VBE.
  7. Save workbook before any other changes. 
Test the code
  1. Ensure Active sheet is the desired sheet.
  2. Press Alt + F8.
  3. Choose 'DeathToApostrophe'.
  4. Press 'Run'.
  5. Confirm with 'Ok' to run.

No comments:

Post a Comment