VBA Code for ComboBox1 to list dates for every Monday

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi good afternoon, hope you can help me please i am trying to make a drop down list where dates can be selected in a ComboBox which is in a Userform for Every Monday up to 8 weeks. and once the old date has expired to disappear from the drop downlist and the new date added weekly. Really hope yuo can help me please i have tried several codes but none are working.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have tried the code below, which works now, but the first date i want it to start from the first Monday from 'Now' if possible then list the next 8 weeks.

if this bit below can be from the first monday from todays date?
VBA Code:
sDate = "Now" & Year(Date)

VBA Code:
Private Sub ComboBox1_Change()


End Sub


Private Sub UserForm_Initialize()

Dim sDate As String
Dim i As Integer

    sDate = "Now" & Year(Date)
    
    If Day(CDate(sDate)) <> vbMonday Then
        sDate = DateAdd("d", vbMonday - Day(CDate(sDate)) - 1, CDate(sDate))
    End If
    
    For i = 0 To 8
        ComboBox1.AddItem Format(DateAdd("ww", i, sDate), "mmm dd, yyyy")
        If Date > DateAdd("ww", i, sDate) And Date < DateAdd("ww", i + 1, sDate) Then
            ComboBox1.ListIndex = i
        End If
    Next i



End Sub
 
Last edited:
Upvote 0
try this update to your code & see if does what you want

VBA Code:
Public Sub UserForm_initialize()
    Dim dDate           As Date
    Dim i               As Long
   
    'change format as required
    Const DateFormat    As String = "mmm dd, yyyy"
    'change no weeks as required
    Const NoWeeks       As Long = 8
    'get date of current week
    dDate = Date - Weekday(Date, vbMonday) + 1
   
    For i = 0 To NoWeeks
        Me.ComboBox1.AddItem Format(DateAdd("ww", i, dDate), DateFormat)
    Next i
   
End Sub

Dave
 
Upvote 0
try this update to your code & see if does what you want

VBA Code:
Public Sub UserForm_initialize()
    Dim dDate           As Date
    Dim i               As Long
  
    'change format as required
    Const DateFormat    As String = "mmm dd, yyyy"
    'change no weeks as required
    Const NoWeeks       As Long = 8
    'get date of current week
    dDate = Date - Weekday(Date, vbMonday) + 1
  
    For i = 0 To NoWeeks
        Me.ComboBox1.AddItem Format(DateAdd("ww", i, dDate), DateFormat)
    Next i
  
End Sub

Dave
HI Dave this is working great thank you how to i change the format to 18/07/2022 (dd,mm,yyyy)?
 
Upvote 0
You are welcome - appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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