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
 
One question: How do we "clean up" repeating dates in the list? For instance, if we have the following dates in D5:D9:

15-Apr-04
15-May-04
15-Jun-04
15-Jul-04
16-Apr-04

with 16-Apr-04 being the date "to include" that we added to the end of the list,

the ARRAY formula in Cells H5:H9:

{=SMALL($D$5:$D$9,{1;2;3;4;5})}

will give us this sorted list:

15-Apr-04
16-Apr-04
15-May-04
15-Jun-04
15-Jul-04

BUT, if we have 15-May-04 as the date that we are adding to the end of our list, then the formula will return this to us:

15-Apr-04
15-May-04
15-May-04
15-Jun-04
15-Jul-04

As we can see, 15-May-04 repeats. It's there twice. Is there a way to see only NON-REPEATING dates in the output list?

Thanks,
~ Monica :help:
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Monica:

One of the ways would be to use AdvancedFilter to extract Unique Values. Post back if you need to discuss this further.
 
Upvote 0
This won't really work in my application, because I have everything set up as formulas. Any idea how to make unique records with formulas?

Thanks,
Mon
 
Upvote 0
And if you don't have or can not install the MoreFuns Add-in, then here is another way ...
Book1
ABCDE
1MonicaDatesUniqueDates4
215-Apr-0404/15/0412
315-May-0405/15/0413
415-May-0406/15/042 
515-Jun-0407/15/0415
615-Jul-04 16
Sheet1 (2)


I have used columns D and E for intermediary calculations.

Formula in cell D2 is ... =COUNTIF($A$2:$A2,$A2)
this is then copied down

Formula in cell E2 is ... =IF(D2=1,ROW(D2),"")
this is then copied down

Formula in cell D1 is ... =COUNTIF(D2:D6,1)

and finally formula in cell C2 is ... =IF(ROWS($1:1)<=$D$1,INDEX($A$1:$A$6,SMALL($E$2:$E$6,ROWS($1:1))),"")

and this is then copied down

I hope this helps!
 
Upvote 0
This is great Yogi! Thank you so much! Is there a way that we can incorporate the formulas in columns C and D in your example into only ONE formula in column C that can handle the whole opertion (this way, we can short-cut the intermediary calculations) in columns C and D?

Thank you in advance! :)
~ Monica
 
Upvote 0
Sorry Yogi!

My last post SHOULD READ:

This is great Yogi! Thank you so much! Is there a way that we can incorporate the formulas in columns D and E in your example into only ONE formula in column C that can handle the whole opertion (this way, we can short-cut the intermediary calculations) in columns D and E?

Thank you in advance! :)
~ Monica
 
Upvote 0
Hi Monica:

Here is a solution with one intermediary column ...
Book1
ABCD
1MonicaDatesUniqueDates4
215-Apr-0404/15/042
315-May-0405/15/043
415-May-0406/15/04 
515-Jun-0407/15/045
615-Jul-04 6
Sheet1 (4)


I still think your best bet is using the AdvancedFilter to extract unique records -- however, you know your project constraints and preferences and I don't.

Anyway, I hope this helps!
 
Upvote 0
Thank you. This brings up another question: What happens if the original values in cells A2:A6 are all unique values. C6 shows a #NUM! error. Any idea how to fix this?

Thanks again,
~Mon

P.S. The AdvancedFilter is accessed through the Data --> Filter --> AdvancedFilter drop down menus? If this is what you're referring to, then it won't work. My solution has to come from the formulas that we're working on. Thanks Yogi! :-D
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,225,292
Messages
6,184,107
Members
453,213
Latest member
redchief

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