PDA

View Full Version : [SOLVED:] copy listbox value(2 of 3 columns) to a listbox that name matches day value (3rd col)



mperrah
09-11-2015, 11:22 AM
I have a listbox (lbBills) with 3 columns and several rows.
I have a 6x7 array matrix of listboxes named dcell1 to dcel42
all on one userform (ufCal)
My lbBills has the day number in 3rd column.

I'm trying to fill the dcell# listbox with the 1st and 2nd column
if the 3rd column matches the dcell#
the D1 is the first day of month so I add that to line up days and values.

if tuesday is first of month D1 = 3 so dcell3 gets 1st of month values

Not sure where I am going wrong here.


Sub insertBills_uf()
Dim c, r, D1, DL As Long
Dim MyDate As Date

With ufCal
MyDate = CDate(.uMonth.Caption & "/1/" & .uYear.Caption)

D1 = FirstWeekDayOfMonth(MyDate) - 1
DL = DaysInMonth(MyDate)

For c = 1 To DL
For r = 0 To .lbBills.ListCount - 1
If .lbBills.List(r, 2) = c Then
If .Controls("dcell" & D1 + c).Value = "" Then
.Controls("dcell" & D1 + c).Value = .lbBills.List(r, 0) & " - " & .lbBills.List(r, 1)
Else
.Controls("dcell" & D1 + c).Value = .Controls("dcell" & D1 + c).Value & vbLf & _
.lbBills.List(r, 0) & " - " & .lbBills.List(r, 1)
End If
End If
Next r
Next c
End With
End Sub

mperrah
09-11-2015, 01:41 PM
This seems to work,
but cant see why the listbox size gets altered (slightly) when adding values.

This is called from a checkbox being checked or unchecked


Sub insertBills_uf()
Dim c, e, r, x, D1, DL As Long
Dim MyDate As Date

With ufCal
MyDate = .uMonth.Caption & "/1/" & .uYear.Caption

D1 = FirstWeekDayOfMonth(MyDate) - 1
DL = DaysInMonth(MyDate)

If .cb_bills.Value = True Then
For x = 1 To 42
.Controls("dcell" & x).Clear
Next x

For r = 0 To .lbBills.ListCount - 1 ' bill list
For c = 1 To DL ' days of month
If .lbBills.List(r, 2) = c Then ' day of bill matches day of month
.Controls("dcell" & D1 + c).AddItem .lbBills.List(r, 0) & " - " & .lbBills.List(r, 1)
End If
Next c
Next r
ElseIf .cb_bills.Value = False Then
For x = 1 To 42
With .Controls("dcell" & x)
For e = UBound(.List) To LBound(.List) Step -1
If .List(e) <> "" Then
.RemoveItem e
End If
Next e
End With
Next x
End If
End With
End Sub