Select month from Data Validation List

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet, in Col A are the dates, in Col B are the hours worked.

I want to show monthly totals in cell I3. For March, the total would be 15.

In G1, I want to show a Data Validation List which ONLY shows the months listed in Col A. In this example, only 01/2023 through 04/2023.

So I guess I need a dynamic Source for that list. How to realise this (with or without VBA) ?
 

Attachments

  • dataval.png
    dataval.png
    37.2 KB · Views: 13

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
If I were you I would input my month numbers into validation list manually like: 1,2,3,4,5,6,7,8,9,10,11,12

Then this formula should work fine:
Excel Formula:
=SUMPRODUCT(--(MONTH(A$2:A$31)=$G$1),B$2:B$31)
 
Upvote 0
Hi,
With VBA:
VBA Code:
Sub MMM()
    With CreateObject("System.Collections.ArrayList")
            For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
                    ss = Format(Cells(i, 1).Value, "mmmm yyyy")
                    If Not .Contains(ss) Then .Add ss
            Next
            .Sort
            ss = Join(.toarray, ",")
    End With
     With [G1].Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=ss
    End With
End Sub
 
Upvote 0
Thanks Flashbond, I guess you will also agree with the code provided by Rustam.

Rustam, is there any way to Sort the list in descending order, i.e. the latest month on top ?
 
Upvote 0
to Sort the list in descending order, i.e. the latest month on top ?
Yes ,
Possible..
VBA Code:
Sub MMM()
    With CreateObject("System.Collections.ArrayList")
            For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
                    ss = Format(Cells(i, 1).Value, "mmmm yyyy")
                    If Not .Contains(ss) Then .Add ss
            Next
            .Sort
            .Reverse
            ss = Join(.toarray, ",")
    End With
     With [G1].Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:=ss
    End With
End Sub
 
Upvote 0
Solution
thanks to both of you. Both solutions are fine, but in this particular case the VBA code better suits my needs.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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