VB code needed for sorting function - please help!

monica S

New Member
Joined
Apr 16, 2004
Messages
39
Hi,

This has been driving me crazy... hopefully someone out there can help.

If I have an ORIGINAL range of dates in cells A1:A5:

22-Apr-04
30-Apr-04
22-May-04
22-Aug-04
22-Sep-04

and a TARGET date of say 16-Jun-04 in cell B1.

I would like to have a FUNCTION that will 'read' the OriginalDates, take the TargetDate into account, and then just INCLUDE or INSERT the target date among this original range of dates. The function would return the new dates in ascending order (i.e. with dates closest to now coming first and dates furthest from now coming last). The function would have to be typed in as an ARRAY function (with the { } bracketing the formula and closing it using Ctrl+Shift+Enter) and would return 1 more row than what is passed through OriginalDates argument.

so, the function would have two arguments and go something like this:

{=IncludeDate(OriginalDates,TargetDate)}

or;

{=(A1:A5,B1)}

In our example, the function would return the following sorted set of dates (with the June 16, 2004 date included and SORTED among the original set of dates):

22-Apr-04
30-Apr-04
22-May-04
*16-Jun-04*
22-Aug-04
22-Sep-04

in cells C1:C6 as a NEW OUTPUT RANGE of dates.

In this case, we would copy our function down 6 rows (one more than the OriginalDates) and close it with the Ctrl+Shift+Enter to make it an ARRAY of 6 rows.

A MACRO WILL NOT HELP IN THIS CASE. ALSO, USING THE DATA --> SORTING FUNCTIONALITY WILL NOT HELP IN THIS CASE. TRUE, BOTH WILL WORK, BUT THIS PROJECT CALLS FOR A **FUNCTION** TO HANDLE THE TASK.

Any ideas on how to go about doing this?

Thank you very much!
~ Monica
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Laurent Longre has already constructed this formula for you. it's called UNIQUEVALUES, which automatically sorts in ascending order for you, numerically AND alphabetically.

try something like this:
Book4
ABCD
122-Apr-0416-Jun-0422-Apr-04
230-Apr-0430-Apr-04
322-May-0422-May-04
422-Aug-0416-Jun-04
522-Sep-0422-Aug-04
622-Sep-04
7 
8 
Sheet2


formula is:

=INDEX(UNIQUEVALUES(A$1:B$5,1),ROW(A2)-ROW(A$1))

copy down.

hth

btw, you can download that add-in for free in the 'Recommended Addins and Links' thread posted by Juan Pablo Gonzalez (upper portion of the 'Excel Questions' forum).
 
Upvote 0
or non-array'd using Yogi's:

=SMALL($A$1:$A$6,ROW(A2)-ROW(A$1))

copy down.

note: this is also using his formula is B6.
 
Upvote 0
Hi Zack:

I think using simply ...

=UNIQUEVALEWS(A1:B5,1)

would also work -- any particular benefit of using the ROW functions in your formulation?
 
Upvote 0
indeed Yogi, coupled with the INDEX function, it's necessity (or a variation of). take these examples:

formula's are:

=SMALL($A$1:$A$6,ROW(A2)-ROW(A$1))

=INDEX(UNIQUEVALUES(A$1:B$5,1),ROW(A2)-ROW(A$1))

=UNIQUEVALUES(A$1:A$6,1)

the last formula does not work on it's own, and does not support 'uniques'.

i use the row to influence the 'row_num' of the INDEX function to ensure the dynamics of the UNIQUEVALUES function.

edit: and hiya Yogi!
 
Upvote 0
Hi there Zack -- Cheers!

So, you are using the iNDEX and the ROW functions to avoid having to use the array formulation -- Right!

I can simplify the formulation ...

=INDEX(UNIQUEVALUES(A$1:B$5,1),ROW(A2)-ROW(A$1))

to

=INDEX(UNIQUEVALUES(A$1:B$5,1),ROWS($A$1:$A1)

or indeed even to ...

=INDEX(UNIQUEVALUES(A$1:B$5,1),ROWS($1:1)

Zack forgive me for just nitpicking now!
 
Upvote 0
Touche!!

and your computations are very good indeed. i guess i prefer to seperate it for troubleshooting simplicity, and going back through my formulas.

nice going though!

take care Yogi! (y)
 
Upvote 0

Forum statistics

Threads
1,216,037
Messages
6,128,442
Members
449,453
Latest member
jayeshw

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