Show number of weeks in combo box

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
Can someone help me with a code that will fill a combo box with the number of weeks in combo for the month I select. September is a combo box named cbMonth to select different months

1599162109495.png
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
testing on a user form I used this
VBA Code:
Private Sub UserForm_Initialize()
    Me.cbMonth.List = Application.GetCustomListContents(4)
End Sub

Private Sub cbMonth_Change()
    Dim dte As Date, i As Long

If cbMonth = "" Then
    cbWeek.Clear
    Exit Sub
End If

dte = Me.cbMonth & " 15, " & Year(Date)
Me.cbWeek.Clear
For i = 1 To WeeksInMonth(dte)
    Me.cbWeek.AddItem "Week" & i
Next i
End Sub

Private Function WeeksInMonth(tDate As Date)
    Dim sDate As Date  ' First of the month
    Dim eDate As Date  ' Last of the month

    sDate = DateSerial(Year(tDate), Month(tDate), 1)
    eDate = DateSerial(Year(tDate), Month(tDate) + 1, 0)
    WeeksInMonth = DateDiff("ww", sDate, eDate) + 1
End Function
 
Upvote 0
Thank you NoSparks for the code. I've been busy at work so I haven't been able to test it, but it works . I greatly appreciate you helping. :)
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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