I am writing some code to have a date pre selected in a set of 3 list boxes and then when the ok button is pressed this date will be added to the excel spreadsheet instead of the date that was originally in the cell. The problem that occurs is that one of the list boxes seems to poulate with the correct number of entries but they are all blank when checking them with msgbox value.
Below is the user form initialize and select button subs
Sub Userform_initialize()
Dim I As Integer
Dim yrtemp As String
Dim V As Integer
'put a double column into the list box
V = lngth - lngthclsd + Numopen ' how many audits to choose from
Selopt = False ' check whether a audit has been selected
ReDim Seldata(1 To V, 1 To 3)
'filling the array
Worksheets(WSnam).Activate
For I = 2 To lngthclsd
If Cells(I, 14) <> "" Then
Seldata(Cells(I, 14), 1) = Cells(I, 3)
Seldata(Cells(I, 14), 2) = Cells(I, 1)
Seldata(Cells(I, 14), 3) = I
End If
Next I
For I = Numopen + 1 To V
Seldata(I, 1) = Cells(I - Numopen + lngthclsd, 3)
Seldata(I, 2) = Cells(I - Numopen + lngthclsd, 1)
Seldata(I, 3) = I - Numopen + lngthclsd
Next I
'displaying the array in the list box
ListBox1.ColumnCount = 2
ListBox1.List = Seldata
With ListBox2
For I = 1 To 31
.AddItem I
Next I
End With
With ListBox3
.AddItem "Jan"
.AddItem "Feb"
.AddItem "Mar"
.AddItem "Apr"
.AddItem "May"
.AddItem "Jun"
.AddItem "Jul"
.AddItem "Aug"
.AddItem "Sep"
.AddItem "Oct"
.AddItem "Nov"
.AddItem "Dec"
End With
With ListBox4
.AddItem Year(Now) - 1
.AddItem Year(Now)
.AddItem Year(Now) + 1
.AddItem Year(Now) + 2
End With
' For I = 1 To 31
' ListBox5.AddItem I
' Next I
'With ListBox5
' For I = 1 To 31
' .AddItem I
' Next I
'End With
'
'With ListBox6
' .AddItem "Jan"
' .AddItem "Feb"
' .AddItem "Mar"
' .AddItem "Apr"
' .AddItem "May"
' .AddItem "Jun"
' .AddItem "Jul"
' .AddItem "Aug"
' .AddItem "Sep"
' .AddItem "Oct"
' .AddItem "Nov"
' .AddItem "Dec"
'End With
With Frame6.ListBox6
For I = 1 To 12
.AddItem Worksheets(WS3nam).Cells(2 + I, 2)
Next I
End With
'With ListBox7
' ' For I = 1 To 4
' .AddItem Year(Now) - 2 + I
' Next I
'End With
End Sub
and the select button
Private Sub Commandbutton4_click()
'select button
Dim I As Integer
Dim II As Integer
Dim dday As Integer
Dim mmnth As Integer
Dim yyr As Integer
Dim yyyr As String
Selopt = True
If ListBox1.ListIndex = -1 Then
MsgBox ("Please Select a Audit to update")
Selopt = False
Exit Sub
End If
I = Seldata(ListBox1.ListIndex + 1, 3)
Worksheets(WSnam).Activate
TextBox1.Text = Cells(I, 5) 'if there is a completed field puts this in form
TextBox2.Text = Cells(I, 6) 'if there is a completed field puts this in form
TextBox3.Text = Cells(I, 7) 'if there is a completed field puts this in form
TextBox4.Text = Cells(I, 16) 'if there is a completed field puts this in form
'MsgBox (I)
dday = Day(Cells(I, 1)) - 1
mmnth = Month(Cells(I, 1)) - 1
'Select Case Month(Cells(I, 1))
'Case Is = 1
' mmnth = "Jan"
'Case Is = 2
' mmnth = "Feb"
'Case Is = 3
' mmnth = "Mar"
'Case Is = 4
' mmnth = "Apr"
'Case Is = 5
' mmnth = "May"
'Case Is = 6
' mmnth = "Jun"
'Case Is = 7
' mmnth = "Jul"
'Case Is = 8
' mmnth = "Aug"
'Case Is = 9
' mmnth = "Sep"
'Case Is = 10
' mmnth = "Oct"
'Case Is = 11
' mmnth = "Nov"
'Case Is = 12
' mmnth = "Dec"
'End Select
dday = Day(Cells(I, 1)) - 1
mmnth = Month(Cells(I, 1)) - 1
yyr = Year(Cells(I, 1)) - Year(Now) + 1
yyyr = Year(Cells(I, 1))
'MsgBox (mmnth)
'MsgBox (yyr)
Frame6.ListBox5.Selected(dday) = True 'puts day in from value
Frame6.ListBox6.Selected(mmnth) = True ' puts month in from value
Frame6.ListBox7.Selected(yyr) = True ' puts year in from value
'MsgBox (ListBox6.ListIndex)
'MsgBox (ListBox7.ListIndex)
MsgBox (ListBox5.Value)
MsgBox (ListBox6.Value)
MsgBox (ListBox7.Value)
'ListBox5.ListIndex = dday
'ListBox6.ListIndex = mmnth
'ListBox7.ListIndex = yyr
'ListBox7.Text = yyyr
'MsgBox (ListBox5.Value)
'MsgBox (ListBox6.Value)
'MsgBox (ListBox7.Value)
'MsgBox (ListBox7.ListCount)
For II = 1 To 31
ListBox5.Selected(II - 1) = True
MsgBox ("'" & ListBox5.Value & "'")
Next II
For II = 1 To 12
ListBox6.Selected(II - 1) = True
MsgBox ("'" & ListBox6.Value & "'")
Next II
For II = 1 To 4
ListBox7.Selected(II - 1) = True
MsgBox ("'" & ListBox7.Value & "'")
Next II
'MsgBox (ListBox5.Value & "-" & ListBox6.Value & "-" & ListBox7.Value - 2000)
End Sub
I have tried a number of ways of getting the data into the list boxes - thus the number of comments. It also doesn't make sense that the problem with the values started with the year(listbox7) and then has moved to the month(listbox6) and the day(listbox5) on occasions Also the other date information (listbox 2,3 and 4) are fine
Thanks for your help in advance
Bookmarks