VBA: Date Format Issues with Data Validation

zero269

Active Member
Joined
Jan 16, 2023
Messages
284
Office Version
  1. 365
Platform
  1. Windows
Hello,

I've got a macro working that creates a Data Validation drop-down list. I duplicated it to repeat the process for a list of three dates.
However, no matter what I do, they never make it into the Data Validation Source field in the format they show in the VBE.

VBE Shows:
1735618322418.png

DV Source:
1735618338383.png

I'm using the following code to build the dates based on the Start Date. Days are then added in a loop using three values in an array (0,7,14).

NOTE:
  • StartDate + Days is just adding the three dates into a String.
  • StartDate + Days & Format Date ATTEMPT is me attempting to force the format output of the dates... to no avail.
My systems default Short Date is yyyy-mm-dd... which is the output format I'm trying to get.

1735619238666.png

The other problem is that it's not seen as a date in Excel (E7).
I'm currently using a formula on another sheet to extract those three dates. Then I'm using the Named Range for those three dates for the DV List. Sample data shown at the bottom.
However, I would prefer to use a vba-based solution if possible.
Any help with getting the DV Source field populated with the three dates in yyyy-mm-dd format would be greatly appreciated. From there, I suspect Excel will properly detect it as a Date (ISNUMBER) format.

Create Date Drop-Down List:

VBA Code:
Sub Planner_List_Dates()

  'Declarations
  Dim StartDate As Date, i As Integer, sDate As String
  Dim DateList As String: DateList = "" 'Date List
  
  'Get Start Date
  StartDate = Range("A2")
'  StartDate = Format(Range("A2"), "yyyy-mm-dd")

  'Days to Add to StartDate in Loop
  Dim indexArr As Variant
  indexArr = Array(0, 7, 14)

  'Build Drop-Down List Values
  For i = LBound(indexArr) To UBound(indexArr)
  
    'StartDate + Days
    DateList = DateList & StartDate + indexArr(i) & ","

    'StartDate + Days & Format Date ATTEMPT
    'sDate = Format(StartDate + indexArr(i), "yyyy-mm-dd")
    'DateList = DateList & sDate & ","
    
  Next i

  'What's happening here?
  DateList = Mid(DateList, 1, Len(DateList) - 1)
  
  'Add Drop-Down List v1
  With Range("E2").Validation
    .Delete
    .Add _
    Type:=xlValidateList, _
    AlertStyle:=xlValidAlertStop, _
    Formula1:=DateList
  End With
  
End Sub

Sample Data for VBA-based Solution:

VBA Testing.xlsm
ABCDEFG
1Start DatePlanner DatesDV List+ Days
22025-01-062025-01-061/6/20250
32025-01-137
42025-01-2014
5
6Date?Date?Date?
7TRUETRUEFALSE
8TRUE
9TRUE
Dates_DV
Cell Formulas
RangeFormula
C2:C4C2=$A$2+$G2
A7,E7A7=ISNUMBER(A2)
C7:C9C7=ISNUMBER($C2)
Cells with Data Validation
CellAllowCriteria
E2List1/6/2025,1/6/2025,1/6/2025


Current Formula-based Solution:
Excel is gonna crash trying to create the MiniSheet... I'll add it below shortly...
 

Attachments

  • 1735618247183.png
    1735618247183.png
    20.5 KB · Views: 3

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
OK, so I still can't get a Mini Sheet from my main workbook right now.

Here's the formula that I'm using on a my Lists worksheet to get the three dates:

Excel Formula:
=CHOOSECOLS(FILTER(t_Planner,(t_Planner[Test Date]=c_Planner_StartDate)+(t_Planner[Test Date]=c_Planner_StartDate+7)+(t_Planner[Test Date]=c_Planner_StartDate+14)),2)

My Planner worksheet is where the drop-down list is located using the Named Range where the above formula is located:
Excel Formula:
=c_Planner_DateList

Name Manager:
Name:
c_Planner_DateList
Comments: Planner dates used for Data Validation on Planner Sheet for filtering books.
Refers To: =Lists!$X$5#
Scope: Workbook
 
Upvote 0
I'm afraid I can only confirm that I can replicate that behaviour and the only option would seem to be similar to what you are already doing ie you would need to populate a range with the required dates and use that range as the validation list. I am not sure if there is any benefit in populating that range using VBA as opposed to your current formula method.
 
Upvote 0
I'm afraid I can only confirm that I can replicate that behaviour and the only option would seem to be similar to what you are already doing ie you would need to populate a range with the required dates and use that range as the validation list. I am not sure if there is any benefit in populating that range using VBA as opposed to your current formula method.
Hi Alex,

That's what I was afraid of. I know something is broken in Excel considering my differences in results. I don't understand how Excel will produce different results using the same input.
Manual was a GO, while VBA was a NO GO. See VBA Recorded Macro remarks below…

I tried using TEXTJOIN for sh!ts-and-giggles, and I surprisingly got some better results, although not a suitable replacement.

Here's a summary of my comparisons:
VBA code provided below…
VBA Testing.xlsm
EFGHIJK
1DV ListDate?CreatedCell FormatList FormatDV SourceMacro
26-JanTRUEManual + Named Rangedynamicdynamic=l_Dates_DVn/a
32025-01-06TRUEManual + Datesdynamicstatic2025-01-06,2025-01-13,2025-01-20n/a
46-JanTRUEVBA + Named Rangedynamicdynamic=l_Dates_DVSub DV_Dates_v1()
52025-01-06TRUEVBA + TEXTJOINdynamicstatic45663,45670,45677Sub DV_Dates_v2()
61/6/2025FALSEVBA Recorded Macron/an/a1/6/2025,1/13/2025,1/20/2025Sub DV_Dates_v3()
71/6/2025FALSEVBA + String of Datesn/an/a1/6/2025,1/13/2025,1/20/2025Sub DV_Dates_v4()
Dates_DV
Cell Formulas
RangeFormula
F2:F7F2=ISNUMBER(E2)
Cells with Data Validation
CellAllowCriteria
E2List=l_Dates_DV
E3List2025-01-06,2025-01-13,2025-01-20
E4List=l_Dates_DV
E5List45663,45670,45677
E6:E7List1/6/2025,1/13/2025,1/20/2025

Summary of my Analysis:
  • Manual + Named Range is obviously the optimal solution until someone can figure out why Excel is broken when using VBA to achieve the same results.
  • Manual + Dates works, but I lose the dynamic number formatting in the drop-down list.
The 1st and 3rd options provide the best results that use a Named Range. Mainly because the drop-down list will display the same Number Format as the source data in the Named Range. This has always been a nice option for me; only because I'm never looking at the year when selecting a date; and the column is more narrow... as preferred.

Manual + Named Range is obviously the optimal solution until someone can figure out why Excel is broken when using VBA to achieve the same results.
VBA Code:
'VBA + Named Range
Sub DV_Dates_v1()
  Range("E4").NumberFormat = "[$-en-US]yyyy-mm-dd;@"
  With Range("E4").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:="=l_Dates_DV"
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
  End With
End Sub
VBA + TEXTJOIN is really the only VBA option where storing the dates is not the preferred approach. This one displays the Date Serial number in the drop down list. This is certainly not acceptable for 99% of use cases. However, for me, I could easily work with this considering I only have three dates in the list… one week apart from the other. Treated as Week 1, Week 2 and Week 3. So, it's not confusing in this one instance.
VBA Code:
'VBA + TEXTJOIN
Sub DV_Dates_v2()
  Range("E5").NumberFormat = "[$-en-US]yyyy-mm-dd;@"
  With Range("E5").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
        Formula1:=WorksheetFunction.TextJoin(",", True, Range("C2:C4"))
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
  End With
End Sub
VBA Recorded Macro: I recorded a macro performing the manual steps that I'm currently using. However, if I run the code, it produces dates in the Source field and Excel rejects it as a Date. So, not useable.
VBA Code:
'Recorded Macro
Sub DV_Dates_v3()
  Range("E6").NumberFormat = "[$-en-US]yyyy-mm-dd;@"
  With Range("E6").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="2025-01-06,2025-01-13,2025-01-20"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
End Sub
VBA + String of Dates: This one produces the same results as the one above. String values; not usable dates.
VBA Code:
'VBA + String of Dates
Sub DV_Dates_v4()
  Dim Dates As String: Dates = "2025-01-06,2025-01-13,2025-01-20"
  Range("E7").NumberFormat = "[$-en-US]yyyy-mm-dd;@"
  With Range("E7").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=Dates
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
  End With
End Sub

Thanks Alex, and best regards,
 
Upvote 0

Forum statistics

Threads
1,225,119
Messages
6,182,955
Members
453,141
Latest member
Owy

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