Excel 使用VBA修改ListBox

Excel 使用VBA修改ListBox,excel,vba,Excel,Vba,我对VBA非常陌生,我一直在试图找到创建日期选择下拉列表的方法,但我没有成功地处理“二月”选择。这是我到目前为止得到的 If (monthListBox.Value = "Jan" Or monthListBox.Value = "Mar" Or monthListBox.Value = "May" Or monthListBox.Value = "Jul" Or monthListBox.Value = "Aug" Or monthListBox.Value = "Oct" Or monthL

我对VBA非常陌生,我一直在试图找到创建日期选择下拉列表的方法,但我没有成功地处理“二月”选择。这是我到目前为止得到的

If (monthListBox.Value = "Jan" Or monthListBox.Value = "Mar" Or monthListBox.Value = "May" Or monthListBox.Value = "Jul" Or monthListBox.Value = "Aug" Or monthListBox.Value = "Oct" Or monthListBox.Value = "Dec") Then
    If dayCount < 31 Then
        For i = dayCount To 31
            dateComboBox.AddItem (i)
        Next i
    End If
ElseIf (monthListBox.Value = "Apr" Or monthListBox.Value = "Jun" Or monthListBox.Value = "Sep" Or monthListBox.Value = "Nov") Then
    dateComboBox.RemoveItem (30)
    If dayCount < 30 Then
        For i = dayCount To 30
            dateComboBox.AddItem (i)
        Next i
    End If
Else
    If isLeapYear = True Then
        With dateComboBox
            .RemoveItem (30)
            .RemoveItem (29)
        End With
    Else
        dateComboBox.RemoveItem (28)
    End If
End If
如果(monthListBox.Value=“Jan”或monthListBox.Value=“Mar”或monthListBox.Value=“May”或monthListBox.Value=“Jul”或monthListBox.Value=“Aug”或monthListBox.Value=“Oct”或monthListBox.Value=“Dec”),则
如果dayCount<31,则
对于i=天数计数到31
dateComboBox.AddItem(一)
接下来我
如果结束
否则(monthListBox.Value=“Apr”或monthListBox.Value=“Jun”或monthListBox.Value=“Sep”或monthListBox.Value=“Nov”),然后
dateComboBox.RemoveItem(30)
如果dayCount<30,则
对于i=天数计数到30
dateComboBox.AddItem(一)
接下来我
如果结束
其他的
如果isLeapYear=True,则
带日期组合框
.RemoveItem(30)
.RemoveItem(29)
以
其他的
dateComboBox.RemoveItem(28)
如果结束
如果结束
试试看

将计算日期系统应用于系统比计算闰年更容易

Private Sub UserForm_Initialize()
    Dim y As Integer
    Dim i As Integer, s As String

    y = 2020 '<~~ your year

    For i = 1 To 12
        s = Format(DateSerial(y, i, 1), "mmm")
        monthListBox.AddItem s
    Next i

End Sub

Private Sub monthListBox_Click()
    Dim s  As String
    Dim y As Integer
    Dim st As Double, et As Double
    Dim i As Integer, cnt As Integer

    y = 2020 '<~~ your year

    s = monthListBox.Value
    st = DateValue(y & "/" & s & "/" & 1)
    et = DateAdd("m", 1, st)
    cnt = et - st

    dateComboBox.Clear
    For i = 1 To cnt
        dateComboBox.AddItem i
    Next i


End Sub
Private子用户表单_Initialize()
Dim y作为整数
Dim i为整数,s为字符串

y=2020'您在处理问题时遇到的具体问题是什么?预期的结果是什么?您得到的结果是什么?只需加上月份名称列表是一个内置函数
MonthName()
,这样第一个循环中的代码就可以是
monthListBox.AddItem MonthName(i)
(或
monthListBox.AddItem Left$(MonthName(i),3)
,仅保留前三个字母)@HTH,在我的国家,它是一个不同的字符。无论如何,谢谢你的新功能。@HTH,在我的国家,MonthName(1)是“1”월".