PDA

View Full Version : [SOLVED:] Formula to find single date from multiple dates



oam
03-06-2023, 01:26 PM
I need a list of each date from column B in the attached sheet. From all the multiple dates I need a list of just one date from each date group.
Column B of attached contains multiples of the same date what I am needing is to have a list of each date form each date range.

Hope this makes sense.
Thank you any and all help

Example of desired result:
1/2/2023
2/3/2023
2/10/2023
2/17/2023

June7
03-06-2023, 02:47 PM
Where do you want this list to be output?

In VBA, add a general module and paste this code:




Sub GetDates()
'code for Excel VBA
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text"""

rs.Open "SELECT DISTINCT [Data$].[Period End Date] FROM [Data$] WHERE NOT [Data$].[Period End Date] IS NULL", cn, adOpenStatic, adLockOptimistic, adCmdText
Worksheets("Data").Range("L2").CopyFromRecordset rs
rs.MoveFirst
Do While Not rs.EOF
Debug.Print rs(0)
rs.MoveNext
Loop
End Sub

Or install Power Query add-in and learn it. I am trying it now for your requirement and it is challenging. I can get it to work but complex enough I don't want to try to describe to you here.

oam
03-06-2023, 03:17 PM
That works but is there not a formula that could do the same?

Thank you for your help

Bob Phillips
03-06-2023, 03:32 PM
Easiest way would be to copy the date column to a blank column, then do Data>Data Tools>Remove Duplicates (don't expand the selection), and that should get you your result.

June7
03-06-2023, 04:10 PM
Okay, Bob, that worked. If this is a one-time occurrence, manual exercise is fine. If this is going to be a recurring event, I would probably use VBA recordset or PowerQuery.

I don't know if there is a formula to accomplish this.

Paul_Hossler
03-06-2023, 05:43 PM
1. VBA



Option Explicit

Sub Macro1()
Worksheets("Data").Range("B:B").Copy Worksheets("Sheet1").Range("A1")
Application.CutCopyMode = False

Application.DisplayAlerts = False
Worksheets("Sheet1").Range("$A:$A").RemoveDuplicates Columns:=1, Header:=xlYes
Application.DisplayAlerts = True
End Sub


2. Worksheet for newer versions of Excel

June7
03-06-2023, 07:43 PM
Okay, Paul, simpler VBA. Nice!

For the formula, you have to put it into as many cells as you expect there would be distinct date values, even in the header row. It's magic! How does it know to return different values? In Excel 2010 it shows as an array formula:

{=_xlfn.UNIQUE(Data!$B1:$B725,FALSE)}

oam
03-07-2023, 05:22 AM
Thank you all for your help!
I will work to integrate them into my sheet and I think Paul's solution is the simplest.

Again, thank you all for your input all ideas could work.

Paul_Hossler
03-07-2023, 08:15 AM
Okay, Paul, simpler VBA. Nice! For the formula, you have to put it into as many cells as you expect there would be distinct date values, even in the header row. It's magic! How does it know to return different values? In Excel 2010 it shows as an array formula: {=_xlfn.UNIQUE(Data!$B1:$B725,FALSE)}

Dynamic arrays were added in later versions

https://exceljet.net/glossary/dynamic-array#:~:text=In%20fall%202018%2C%20Microsoft%20announced,in%20a%20%22spill %20range%22.

https://exceljet.net/dynamic-array-formulas-in-excel

https://a4accounting.com.au/new-formula-symbol-in-excel/



The # symbol follows the reference and it makes it incredibly easy to refer to spilled ranges. You use the # after the reference to the cell in the top left corner of the spilled range.

The @ symbol precedes the reference and it forces Excel to treat the reference as it would have pre-dynamic arrays.


Attached a workbook I was using to investigate



As of January 2023, many more new functions have now been released to take advantage of the dynamic array engine. The complete list of new functions is: ARRAYTOTEXT (https://exceljet.net/functions/arraytotext-function), BYCOL (https://exceljet.net/functions/bycol-function), BYROW (https://exceljet.net/functions/byrow-function), CHOOSECOLS (https://exceljet.net/functions/choosecols-function), CHOOSEROWS (https://exceljet.net/functions/chooserows-function), DROP (https://exceljet.net/functions/drop-function), EXPAND (https://exceljet.net/functions/expand-function), FILTER (https://exceljet.net/functions/filter-function), HSTACK (https://exceljet.net/functions/hstack-function), ISOMITTED (https://exceljet.net/functions/isomitted-function), LAMBDA (https://exceljet.net/functions/lambda-function), LET (https://exceljet.net/functions/let-function), MAKEARRAY (https://exceljet.net/functions/makearray-function), MAP (https://exceljet.net/functions/map-function), RANDARRAY (https://exceljet.net/functions/randarray-function), REDUCE (https://exceljet.net/functions/reduce-function), SCAN (https://exceljet.net/functions/scan-function), SEQUENCE (https://exceljet.net/functions/sequence-function), SORT (https://exceljet.net/functions/sort-function), SORTBY (https://exceljet.net/functions/sortby-function), STOCKHISTORY (https://exceljet.net/functions/stockhistory-function), TAKE (https://exceljet.net/functions/take-function), TEXTAFTER (https://exceljet.net/functions/textafter-function), TEXTBEFORE (https://exceljet.net/functions/textbefore-function), TEXTSPLIT (https://exceljet.net/functions/textsplit-function), TOCOL (https://exceljet.net/functions/tocol-function), TOROW (https://exceljet.net/functions/torow-function), UNIQUE (https://exceljet.net/functions/unique-function), VALUETOTEXT (https://exceljet.net/functions/valuetotext-function), VSTACK (https://exceljet.net/functions/vstack-function), WRAPCOLS (https://exceljet.net/functions/wrapcols-function), WRAPROWS (https://exceljet.net/functions/wraprows-function), XLOOKUP (https://exceljet.net/functions/xlookup-function), and XMATCH (https://exceljet.net/functions/xmatch-function).

June7
03-07-2023, 12:20 PM
Correction, in Access 2010 it showed the same formula in each cell but doesn't seem to work. Here is method for older versions.

Get a list of unique values in Excel & extract unique rows (ablebits.com) (https://www.ablebits.com/office-addins-blog/get-unique-distinct-values-excel/#:~:text=The%20detailed%20explanation%20of%20the%20formula%27s%20logic%20is ,as%20needed%20by%20dragging%20the%20fill%20handle.%20)

Miranda34
11-28-2023, 12:15 AM
Correction, in Access 2010 it showed the same formula in each cell but doesn't seem to work. Here is method for older versions.

Get a list of unique values in Excel & extract unique rows (ablebits.com) (https://www.ablebits.com/office-addins-blog/get-unique-distinct-values-excel/#:~:text=The%20detailed%20explanation%20of%20the%20formula%27s%20logic%20is ,as%20needed%20by%20dragging%20the%20fill%20handle.%20)
This fix is so much better, thank you