Change Data Time Format and Identify last row.

LeanneBG

Board Regular
Joined
Jun 20, 2016
Messages
157
Hi Experts - I have below code that sorts the data from column A to K based on the date and time value found in column I.. the code works, however, i feel like i can do better on this part of the code - "I1:I2000" My number of my rows are not fixed and is changing.. so not sure how to do this.. Also sometimes the table has no data so i want my code to not end in error in case there's no data..

Also, before sorting the date and time, i need to change the format first from 2019-03-15 11:52:18 into this format 15/03/2019 11:41:10 AM. Again the number of rows is not fixed so i dont have the definite last row for it.. but i need to change the format of the whole column I until the row with the last data..
Thanks in advance for those who will help..

Code:
Sub Sort()
    Sheets("SRPT").Select
    ActiveSheet.AutoFilterMode = False
    
    Range("A1").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("SRPT").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("SRPT").AutoFilter.Sort.SortFields.Add Key:=Range( _
        "I1:I2000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("SRPT").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
End Sub
 
Hi LeanneBG,

How are your dates formatted?

As dates or as text?

if stored as text you will have to convert them to dates first.

Sometimes you can identity dates as text (Provided you have'nt right aligned the cells first) if left aligned store as text, if right aligned stored as dates) when imported data from a file or database they come in as text and not dates.

What are yours stored as?

Everytime i manually change the format to dates (long or short), nothing is happening.. It is only changing to the format i want everytime i click after the last number and click enter..
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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