Sort by Date

Status
Not open for further replies.

trevormay99

New Member
Joined
Aug 25, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I have a macro that moves data for the upcoming week to the top of selected spreadsheets, the code is as follows


VBA Code:
   Dim currentDate         As Date
    Dim upcomingWeekEnd     As Date
    Dim ArrayRow            As Long
    Dim HelperColumnNumber  As Long
    
    Dim DateColumnArray     As Variant, HelperColumnArray   As Variant
    Dim sheetNames          As Variant
   
'
    currentDate = Date                                                                      ' Save today's date to currentDate
    upcomingWeekEnd = currentDate + 7                                                       ' Save the date that is 7 days from today to upcomingWeekEnd
'
    ' Loop through all worksheets in the workbook
    For Each ws In ThisWorkbook.Worksheets                                                  ' Loop through the sheets in the workbook
        Select Case ws.Name
            Case Is = "Prod Meet", "Elec Meet", "Mech Meet"                                 '       If the sheet name matches a sheet that we are looking for then ...
                With ws
                    lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row                        '               Get the lastRow of the date column
                    .Range("A1").AutoFilter                                                 '               Turn on the filter
                    .Range("A1:L" & lastRow).Sort Key1:=.Range("F1"), Order1:=xlAscending, _
                            Header:=xlYes                                                   '               Filter the rows of data ascending according to the dates in column F
                    .AutoFilterMode = False                                                 '               Turn off the filter
'
                    HelperColumnNumber = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 2 '             Get HelperColumnNumber to use
                    DateColumnArray = .Range("F2:F" & lastRow).Value2                       '               Save the dates from column F into DateColumnArray
                End With
'
                ReDim HelperColumnArray(1 To UBound(DateColumnArray), 1 To 1)               '           Establish the dimensions of the HelperColumnArray
'
                For ArrayRow = 1 To UBound(DateColumnArray)                                 '           Loop thru rows of DateColumnArray
                    If DateColumnArray(ArrayRow, 1) >= currentDate And _
                            DateColumnArray(ArrayRow, 1) <= upcomingWeekEnd Then            '               If the date is within the date range then ...
                        HelperColumnArray(ArrayRow, 1) = 1                                  '                   Save '1' to the HelperColumnArray
                    End If
                Next                                                                        '           Loop back
'
                With ws.Range("A2").Resize(UBound(DateColumnArray), HelperColumnNumber)     '
                    .Columns(HelperColumnNumber).Value = HelperColumnArray                  '               Write the HelperColumnArray to the HelperColumnNumber
                    .Sort Key1:=.Columns(HelperColumnNumber), Order1:=xlAscending, _
                            Header:=xlNo                                                    '               Sort the Rows with '1's to the top
                    .Columns(HelperColumnNumber).Delete                                     '               Delete the HelperColumn
                End With
        End Select
    Next

However I also have a labor hour summation next to my data table, when this code runs and moves the rows of data that fit the timeframe to the top of the sheet it pushes the hour summation down is there any way to prevent this from happening? As it makes the sheet look messy.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Duplicate to: Sort by Date

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top