VBA/Macro to sort rows by first date in multipe columns?

Verona1108

New Member
Joined
Feb 23, 2013
Messages
1
I am putting together a spreadsheet to help a friend at work. She will have five columns of potential dates. Sometimes there may be only one date in a row or there could be 3 or 4. I need a macro that will automatically sort the rows, based on the first date in one of the five columns. If the first column is blank, it would look in the second column, etc to sort by the first available date.

Sample of data unsorted

Name
Description
Date 1
Date 2
Date 3
Date 4
Date 5
BobSample 14/15/136/5/13
SarahSample 25/1/135/15/137/20/13
JohnSample 33/7/13
ScottSample 42/15/134/12/13

<tbody>
</tbody>

Sample of data sorted

Name
Description
Date 1
Date 2
Date 3
Date 4
Date 5
ScottSample 42/15/134/12/13
JohnSample 33/7/13
BobSample 14/15/136/5/13
SarahSample 25/1/135/15/137/20/13

<tbody>
</tbody>

I have very basic experience with Macros and VBA. I know how to add them and make simple edits as long as I know what the references are for. Any help would be greatly appreciated. I have been searching forever for a sample similar to this and have not had any luck. Maybe I am using the wrong terms.

Thanks in advance for any help!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Give this a try:

Code:
Sub firstdate()
Dim sh As Worksheet, lr As Long
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Columns("c").Insert
For i = 2 To lr
With sh
If .Cells(i, 4) <> "" Then
.Cells(i, 3) = .Cells(i, 4).Value
Else
.Cells(i, 3) = .Cells(i, 3).End(xlToRight).Value
End If
End With
Next
sh.Range("A1").CurrentRegion.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlYes
Columns("C").Delete
End Sub
Code:
 
Upvote 0
Hello and welcome,

I may have missed the point and grossly oversimplified this, but the code below seems to do what you ask.
Please try it on a sample of your data.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> testdate()<br>  lr = Cells(Rows.Count, "A").End(xlUp).Row<br>  Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Worksheets(1)<br>      .Range("H2:H" & lr).Formula = "=Min(B2:G2)"<br>      .Range("A2:H" & lr).Sort key1:=Range("H2"), _<br>        Orientation:=xlTopToBottom, Header:=xlYes<br>      .Range("H2:H" & lr).Clear<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>   Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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