Datum opsplitsen vanuit Combobox

Status
Niet open voor verdere reacties.

mark1987vw

Gebruiker
Lid geworden
19 mei 2009
Berichten
96
Hallo,

Ik heb een combobox die gevuld wordt met datums.
om precies te zijn de datums van 7 dagen terug.

Code:
Private Sub UserForm_Initialize()

Dim i As Integer, myDate As Date
    
    myDate = Now() - 7
    
        For i = 0 To 6
            ComboBox1.AddItem Format(DateAdd("d", i, myDate), "dddd d mmmm yyyy")
        
        Next
            ComboBox1.ListIndex = 0

End Sub

Nu wil ik de waarde van de de geselecteerde datum opsplitsen.

Dus:

Dag(naam)
Dag(Nummer)
Maand
Jaar
Weeknummer

Alleen hij blijft de gehele datum werkgeven terwijl ik wel een format erop toepas.

Hierbij de code en heb ook een voorbeeld bestand bijgevoegd.

Code:
Private Sub ComboBox1_Change()


Dagnummer.Caption = Format(ComboBox1.Value, "d")
Maand.Caption = Format(ComboBox1.Value, "mmmm")
Jaar.Caption = Format(ComboBox1.Value, "yyyy")
Weeknummer.Caption = Format(ComboBox1.Value, "ww")
Dagnaam.Caption = Format(ComboBox1.Value, "dddd")


End Sub
 

Bijlagen

  • Datums-Combobox.xlsm
    14,3 KB · Weergaven: 17
Je maakt het te mooi voor jezelf:
Code:
    myDate = Now() - 7
        For i = 0 To 6
            ComboBox1.AddItem DateAdd("d", i, myDate)
        Next
        ComboBox1.ListIndex = 0
 
Bedankt voor je snelle antwoord.

De combobox kan ik dus niet op een 'nettere' manier weergeven ?
 
Jawel, zo:
Code:
Private Sub UserForm_Initialize()
Dim i As Integer, myDate As Date
Dim arr(6, 4) As Variant
    
    myDate = Now() - 7
        For i = 0 To 6
            arr(i, 0) = DateAdd("d", i, myDate)
            arr(i, 1) = Format(DateAdd("d", i, myDate), "dddd")
            arr(i, 2) = Format(DateAdd("d", i, myDate), "d")
            arr(i, 3) = Format(DateAdd("d", i, myDate), "mmmm")
            arr(i, 4) = Format(DateAdd("d", i, myDate), "yyyy")
        Next i
        With Me.ComboBox1
            .List = arr
            .ListIndex = 0
        End With


End Sub
Dan moet je wel het aantal kolommen vergroten, en de opmaak aanpassen. Ik heb 'm zo, en dat ziet er netjes uit:
Code:
100 pt;49,95 pt;24,95 pt;49,95 pt;24,95 pt
 
Vermijd Additem om een combobox of listbox te vullen.

Dit is voldoende:

Code:
Private Sub UserForm_Initialize()
   ComboBox1.List = [index(text(today()+row(1:7)-1,"[$-413]dddd dd mmmm yyyy ") & weeknum(today()+row(1:7)-1,21),)]
End Sub
Code:
Private Sub ComboBox1_Change()
  If ComboBox1.ListIndex > -1 Then
     st = Split(ComboBox1.Text)
     Dagnaam = st(0)
     Dagnummer = st(1)
     Maand = st(2)
     Jaar = st(3)
     Weeknummer = st(4)
  End If
End Sub
Zie ook: https://www.snb-vba.eu/VBA_Fill_combobox_listbox.html
 
Laatst bewerkt:
En je click event kan dan ook anders:
Code:
Private Sub ComboBox1_Click()
    Dagnummer.Caption = Me.ComboBox1.Column(2)
    Maand.Caption = Me.ComboBox1.Column(3)
    Jaar.Caption = Me.ComboBox1.Column(4)
    Weeknummer.Caption = Format(Me.ComboBox1.Value, "ww")
    Dagnaam.Caption = Me.ComboBox1.Column(1)
End Sub
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan