title: Sorting Multiple Columns in VBA
body: <p>I'm having a hard time trying to sort my columns. Should seem like an easy fix but I keep getting stuck. My columns are set up so that when it first populates, Column A might have values in row 3 6 9 12, Column B might have values in 1 2 3 4 and C might have values in 10 20 and 30. I'm trying to go from Column A to Column C and recursively sorting it alphabetically. I have hardcoded as a sub but everytime it gets to a column without values it ends the the program entirely. This is what I have so far</p>
  
  <pre><code>ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _
      Range("G1:G3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
      :=xlSortNormal
  With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  
  
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _
      Range("H1:H3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
      :=xlSortNormal
  With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  
  
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _
      Range("I1:I3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
      :=xlSortNormal
  With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  
  
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
  ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add Key:= _
      Range("J1:J3000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
      :=xlSortNormal
  With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  </code></pre>
  
  <p>I'm calling the function on two sheets. On sheet 1 it has values up to J but on sheet 2 it only has values up to I and it gets stuck giving me an error on Sheet 2 because theres nothing on J. How couple I incorporate a for loop and how would my program know if there is/is not a value in the next column?</p>
  
  <pre><code>       Column A    Column B    Column C    Column D    Column E
  Row 1   Bread       Fruit
  Row 2   Apple        OJ         Coffee  
  Row 3   Banana      Bread       Lotion      Water
  Row 4   Soda            
  Row 5   Fruit       Coke        
  Row 6   Coffee      Tea     
  </code></pre>