populate date (MM-YYYY) in combobox on userform

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
Office Version
  1. 2016
Platform
  1. Windows
I want populating date in combobox on userform like this JAN-2022, FEB 2022 ......
JAN-2023, FEB-2023 ..... and so on based on column G
with considering there is duplicates dates for the same month then should not repeat MM-YYYY based on column G
here is what I have but doesn't work
VBA Code:
Private Sub UserForm_Initialize()
Dim c As Range, LR As Long
LR = sheet1.Range("G" & Rows.Count).End(xlUp).Row
For Each c In sheet1.Range("G" & LR)
ComboBox1.AddItem Format(c.Value, "mm-yyyy")
Next c
End Sub
I hope somebody has idea to do that.
 
Hi KalilMe,

change codeline to read

VBA Code:
      dTally.Add sTemp, 1

and be aware that it's a String that is shown there. In order to use it as a Date you may use DateValue("01-" & ComboBox1.Value).

Holger
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
thanks it works excellently
about this
In order to use it as a Date you may use DateValue("01-" & ComboBox1.Value).
could show me where put this line ?
I still don't understand what could show .
 
Upvote 0
Hi KalilMe,

it depends on what you use the CB for. My remark is meant to be used as part of an AutoFilter for the data (such as filter the data to show all records for corresponding month).

I choose the Change-Event for the CB to trigger the code:

VBA Code:
Private Sub ComboBox1_Change()
Dim dteStart As Date

dteStart = DateValue("01-" & ComboBox1.Value)
With Tabelle1
  If .AutoFilterMode Then .AutoFilterMode = False
  .Rows(1).AutoFilter Field:=7, _
                      Criteria1:=">=" & CLng(dteStart), _
                      Operator:=xlAnd, _
                      Criteria2:="<=" & CLng(DateSerial(Year(dteStart), Month(dteStart) + 1, 0))
End With
End Sub

On any change of the ComboBox the data in the worksheet is filtered accordingly.

Holger
 
Upvote 0
@HaHoBe
I thought you mean I should just modify some line based on your suggestion , but I see this is different what I look for it.
thanks very much for your solution ;)
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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