title: Excel VBA macro to sort alphabetically before save
body: <p>In Excel, I'm trying to enter data in a new row, and when I save, have it automatically sort all populated rows alphabetically by the values in column A (essentially, I want to remove the hassle of clicking "Sort A to Z" before each save). I have the following in the code of the worksheet (the workbook contains three worksheets, and I only want this macro to operate for one of them)</p>
  
  <pre><code>Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      On Error Resume Next
      If Not Intersect(Target, Range("A:A")) Is Nothing Then
          Range("A1").Sort Key1:=Range("A3"), _
            Order1:=xlAscending, _
            MatchCase:=False, _
            Orientation:=xlTopToBottom
      End If 
  End Sub
  </code></pre>
  
  <p>Note: The key1 range starts at A3 because rows 1-2 are headers. Can anyone figure out why this code isn't working? I don't really code so if something's missing I would have no idea. I know it's not an issue with macros being enabled because I can run other macros manually with no problem, it's just this automatic one that isn't working.</p>