Start value listbox

H

Henk

On a form I have 2 (as far as I can see exactly the same) ListBoxes. I fill
both ListBoxes using .AddItem with dates from today up to December 31 of this
year. For both ListBoxes I set the .ListIndex to 0. When I display the values
of both the ListBoxes in a MsgBox, I only see one (the second) value. Anyone
any idea what can be the cause of this?

I tried to force the ListBox to have a value with .LisBox.Value = , but that
did not help.

Many thanks in advance.
 
H

Henk

This is the code :

With BlockDay

.BlockDayStartDate.Clear
.BlockDayEndDate.Clear
Today = Sheets("ResAlloqBase").Range("TodaysDate").Value
For x = 1 To 366
DayNumber = Weekday(Today)
Select Case DayNumber
Case Is = 1
TodaysDay = "Sunday"
Case Is = 2
TodaysDay = "Monday"
Case Is = 3
TodaysDay = "Tuesday"
Case Is = 4
TodaysDay = "Wednesday"
Case Is = 5
TodaysDay = "Thursday"
Case Is = 6
TodaysDay = "Friday"
Case Is = 7
TodaysDay = "Saturday"
End Select

.BlockDayStartDate.AddItem (Today & " " & TodaysDay)
.BlockDayEndDate.AddItem (Today & " " & TodaysDay)

Today = Today + 1

If Month(Today) = 12 And Day(Today) = 31 Then
x = 365
End If

Next x

.BlockDayStartDate.ListIndex = 0
.BlockDayEndDate.ListIndex = 0
.BlockDayMondays.Value = True
.BlockDayTuesdays.Value = True
.BlockDayWednesdays.Value = True
.BlockDayThursdays.Value = True
.BlockDayFridays.Value = True
.BlockDaySaturdays.Value = True
.BlockDaySundays.Value = True

.BlockDayBlockDay.Value = True

MsgBox(.BlockDayStartDate & " " & .BlocDayEndDate)

.Show

End With
 
D

Dave Peterson

I put a couple of listboxes on a small userform and used this code:

Option Explicit
Private Sub UserForm_Initialize()
Dim dCtr As Long
Dim StartDate As Date

StartDate = Date

For dCtr = StartDate To DateSerial(Year(StartDate), 12, 31)
Me.ListBox1.AddItem Format(dCtr, "mm/dd/yyyy dddd")
Me.ListBox2.AddItem Format(dCtr, "mm/dd/yyyy dddd")
Next dCtr

End Sub
 
H

Henk

Dave,

Thanks for your prompt answer. I started this code using date formats as
well, but since VB did not let me set a start value for the listbox, I
started to look for workarounds. I understand your code, and implemented it
in my code as herunder, but I still see only one date in my MsgBox..........

Dim dCtr As Long
Dim StartDate As Date

StartDate = Date

With BlockDay

For dCtr = StartDate To DateSerial(Year(StartDate), 12, 31)
.BlockDayStartDate.AddItem Format(dCtr, "mm/dd/yyyy dddd")
.BlockDayEndDate.AddItem Format(dCtr, "mm/dd/yyyy dddd")
Next dCtr


.BlockDayStartDate.ListIndex = 0
.BlockDayEndDate.ListIndex = 0
.BlockDayMondays.Value = True
.BlockDayTuesdays.Value = True
.BlockDayWednesdays.Value = True
.BlockDayThursdays.Value = True
.BlockDayFridays.Value = True
.BlockDaySaturdays.Value = True
.BlockDaySundays.Value = True

.BlockDayBlockDay.Value = True

MsgBox(.BlockDayStartDate & " " & .BlockDayEndDate)

.Show

End With
 
D

Dave Peterson

I think it's a timing issue.

When the Userform was shown, both listboxes had the top item selected.

Option Explicit
Private Sub UserForm_Click()
With Me
MsgBox "Start: " & .BlockDayStartDate.Value _
& "--End: " & .BlockDayEndDate.Value
End With
End Sub
Private Sub UserForm_Initialize()
Dim dCtr As Long
Dim StartDate As Date

StartDate = Date

With Me
For dCtr = StartDate To DateSerial(Year(StartDate), 12, 31)
.BlockDayStartDate.AddItem Format(dCtr, "mm/dd/yyyy dddd")
.BlockDayEndDate.AddItem Format(dCtr, "mm/dd/yyyy dddd")
Next dCtr

.BlockDayStartDate.ListIndex = 0
.BlockDayEndDate.ListIndex = 0
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top