VBA Excel 2010 ComboBox和Select Case

时间:2014-02-01 09:57:40

标签: vba excel-vba excel-2010 excel

很抱歉打扰你,我一直在阅读和搜索谷歌,但无法找到我的答案......

我确信我已经看到了答案,但它没有意义......

我有一个按钮来加载日期列表,我希望你能看到以下......从当前日期开始的7天......

我的comboBox名称是“cmbDiffDay”我可以使用“.AddItem”成功添加日期...所以当我点击列表时我可以看到日期...所以我很高兴他们大声笑.... / p>

我想为comboBox使用“Select Case”,但我不知道如何让它工作?

Sub Load7DayCount_Click()

Dim DayMinus1 As Date
Dim DayMinus2 As Date
Dim DayMinus3 As Date
Dim DayMinus4 As Date
Dim DayMinus5 As Date
Dim DayMinus6 As Date
Dim DayMinus7 As Date

DayMinus1 = DateAdd("d", -1, Date)
DayMinus2 = DateAdd("d", -2, Date)
DayMinus3 = DateAdd("d", -3, Date)
DayMinus4 = DateAdd("d", -4, Date)
DayMinus5 = DateAdd("d", -5, Date)
DayMinus6 = DateAdd("d", -6, Date)
DayMinus7 = DateAdd("d", -7, Date)

With Sheet1.cmbDiffDay

 .Clear
 .AddItem "- Today " & Date & " - "
 .AddItem "- 1:  " & DayMinus1
 .AddItem "- 2:  " & DayMinus2
 .AddItem "- 3:  " & DayMinus3
 .AddItem "- 4:  " & DayMinus4
 .AddItem "- 5:  " & DayMinus5
 .AddItem "- 6:  " & DayMinus6
 .AddItem "- 7:  " & DayMinus7

End With


Select Case ???????

 Case DayMinus1
  MsgBox "Selected minus date 1"

 Case DayMinus2
  MsgBox "Selected minus date 2"

 Case DayMinus3
  MsgBox "Selected minus date 3"

 Case DayMinus4
  MsgBox "Selected minus date 4"

 Case DayMinus5
  MsgBox "Selected minus date 5"

 Case DayMinus6
  MsgBox "Selected minus date 6"

 Case DayMinus7
  MsgBox "Selected minus date 7"

End Select

End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用(未​​测试)

    If InStr(1, Load7DayCount.Text, ":") Then
        sTemp = Trim(Split(Load7DayCount.Text, ":")(1))
    Else
        sTemp = Load7DayCount.Text
    End If

    Select Case sTemp
        Case DayMinus1: MsgBox "Selected minus date 1"
        Case DayMinus2: MsgBox "Selected minus date 2"
        Case DayMinus3: MsgBox "Selected minus date 3"
        Case DayMinus4: MsgBox "Selected minus date 4"
        Case DayMinus5: MsgBox "Selected minus date 5"
        Case DayMinus6: MsgBox "Selected minus date 6"
        Case DayMinus7: MsgBox "Selected minus date 7"
        Case Else: MsgBox "Selected First option" '<~~ Optional
    End Select

但是就像我上面提到的那样,如果你将.AddItem "- Today " & Date & " - "更改为.AddItem "- Today:" & Date & " - ",那么你可以使用下面的

    Select Case Trim(Split(Load7DayCount.Text, ":")(1))
        Case DayMinus1: MsgBox "Selected minus date 1"
        Case DayMinus2: MsgBox "Selected minus date 2"
        Case DayMinus3: MsgBox "Selected minus date 3"
        Case DayMinus4: MsgBox "Selected minus date 4"
        Case DayMinus5: MsgBox "Selected minus date 5"
        Case DayMinus6: MsgBox "Selected minus date 6"
        Case DayMinus7: MsgBox "Selected minus date 7"
        Case Else: MsgBox "Selected First option" '<~~ Optional
    End Select

修改

您也可以使用.ListIndex属性。例如

Select Case Load7DayCount.ListIndex
    Case -1: MsgBox "You didn't select anything"
    Case 0: MsgBox "You selected first option"
    Case 1: MsgBox "Selected minus date 1"
    Case 2: MsgBox "Selected minus date 2"
    '
    '~~> And So On
    '
End Select

答案 1 :(得分:0)

我不确定Select Case是否可以到达此处。也许这只是示例文本,并不代表您要完成的任务。但根据您提供的信息,我建议使用循环和ListIndex而不是Select Case。

Private Sub cmbDiffDay_Change()

    If Me.cmbDiffDay.ListIndex = 0 Then
        MsgBox "Today"
    Else
        MsgBox "Selected minus date " & Me.cmbDiffDay.ListIndex
    End If

End Sub

Private Sub UserForm_Initialize()

    Dim i As Long

    Me.cmbDiffDay.AddItem "- Today " & Date & " - "

    For i = 1 To 7
        Me.cmbDiffDay.AddItem "- " & i & ":  " & Date - i
    Next i

End Sub