Forum Discussion

stephanieporter's avatar
stephanieporter
Copper Contributor
Jul 03, 2020
Solved

Help with date sorting macro

Hi all, 

 

Hoping to get some help on a macro in order to sort an entire worksheet by date. 

I have multiple rows and the first column is the date (using a date and time picker macro). People will be going in and filling out the spreadsheet, but with each row of information, the date of that item may change to a later date. I want to have a macros where by when the date of a row is changed in column A, it moves the entire row up or down to order it in the order of the date. Does this make sense? 

 

The one macro I found is below, but I'm not sure how to change it in order to read the entire sheet and move the rows. I also already have a date and time picker macro in the sheet, so wondering would I just paste this below it? Any help? 

 

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160606
    On Error Resume Next
    If Application.Intersect(Target, Application.Columns(1)) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _
                                        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
 
  • DevendraJain's avatar
    DevendraJain
    Jul 08, 2020

    stephanieporter Please check the following video and following excel whether I understood correctly what you wanted to say. 

     

    And still if you have any query in VBA then you can also message me privately or on my mail id

     

    And if you liked my answer please do mark my answer as best answer officially

4 Replies

  • DevendraJain's avatar
    DevendraJain
    Iron Contributor

    stephanieporter Hey I have created a simpler Macro you can try this Macro

     

    Sub Macro2()
    '
    ' Macro2 Macro
    '

    '
    Sheets("Sheet1").Select
    Cells.Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
    ("A1:A1048576"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Range("A1").Select
    Selection.AutoFilter
    End Sub

     

    I am also attaching the file you can check whether it is running

    • stephanieporter's avatar
      stephanieporter
      Copper Contributor

      DevendraJain 

       

      Ok so not sure it's working. The code thats currently in my work book (for column a to have date and time pickers in it) is: 


      Private Sub DTPicker1_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)

      End Sub

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)

      With Sheet1.DTPicker1
      .Height = 20
      .Width = 20
      If Not Intersect(Target, Range("A115:A120,A122:A131")) Is Nothing Then
      .Visible = True
      .Top = Target.Top
      .Left = Target.Offset(0, 1).Left
      .LinkedCell = Target.Address
      Else
      .Visible = False
      End If
      End With

      End Sub

       

      I now need something to sort all the rows by date should we decide to change the date on something, I want it to automatically move all the content and align them in date order (if possible). There are 17 columns of information that all needs to stick together. Am I making sense? I can't post a copy of the work book as it's confidential information. 

       

      Thanks

      • DevendraJain's avatar
        DevendraJain
        Iron Contributor

        stephanieporter Please check the following video and following excel whether I understood correctly what you wanted to say. 

         

        And still if you have any query in VBA then you can also message me privately or on my mail id

         

        And if you liked my answer please do mark my answer as best answer officially

    • stephanieporter's avatar
      stephanieporter
      Copper Contributor

      Thank you! DevendraJain 

       

      If I have another Macro on the worksheet, do I just copy and paste this underneath it in the coding section? 

Resources