Microsoft Excel

Excel und VBA: Formeln, Programmierung, Lösungen

Die Excel/VBA-Beispiele

Thema

Alle ComboBoxes auf Position 1

Gruppe

ComboBox

Problem

Wie kann ich bei EXCEL 5.0/7.0 in einer Prozedur festlegen, daß bei Aufruf eines Dialogs alle Dropdowns das erste Element der Liste anzeigen?

Lösung
Geben Sie den nachfolgenden Code in das Klassenmodul der UserForm ein.

StandardModule: basMain

Sub CallForm()
   frmDate.Show
End Sub

ClassModule: frmDate

Private Sub cboMonth_Change()
   Call SetDays
End Sub

Private Sub cboYear_Change()
   Call SetDays
End Sub

Private Sub cmdContinue_Click()
   Unload Me
End Sub

Private Sub UserForm_Initialize()
   Dim lDay As Long
   Dim iYear As Integer, iMonth As Integer
   For iYear = Year(Date) - 3 To Year(Date) + 2
      cboYear.AddItem iYear
   Next iYear
   For iMonth = 1 To 12
      cboMonth.AddItem Format(DateSerial(1, iMonth, 1), "mmmm")
   Next iMonth
   cboYear.ListIndex = 0
   cboMonth.ListIndex = 0
   Call SetDays
End Sub

Private Sub SetDays()
   Dim lDay As Long
   cboDay.Clear
   For lDay = DateSerial(cboYear.Value, cboMonth.ListIndex + 1, 1) To _
      DateSerial(cboYear.Value, cboMonth.ListIndex + 2, 0)
      cboDay.AddItem Day(lDay)
   Next lDay
   cboDay.ListIndex = 0
End Sub