I have a userform (CalendarOpts) that gets a 3 column listbox (lbBills)
populated from a range on my main sheet B2 to D & lastrow
On the userform I allow the user to add, remove and modify the listbox items.
Then we can generate a calendar that gets populated with the listbox values.
My problem is even if the values have been modified, the calendar seems to get the original initialized values, not the modified list.
Is there a way to register in memory (or what ever is necessary) the new values from the userform
This is the initialize code:
Private Sub UserForm_Initialize()
Dim lbtarget As MSForms.ListBox
Dim rngSource As Range
Dim lrB As Integer
Dim wsB As Worksheet
Set wsB = Sheets("Bills")
lrB = wsB.Cells(Rows.Count, 2).End(xlUp).Row
Set rngSource = wsB.Range("B2:D" & lrB)
Me.lblMonth.Caption = wsB.Range("cMonth").Value
Me.sbMonth.Value = wsB.Range("cMonth").Value
Me.lblYear.Caption = wsB.Range("cYear").Value
Me.sbYear.Value = wsB.Range("cYear").Value
Set lbtarget = Me.lbBills
With lbtarget
.ColumnCount = 3
.ColumnWidths = "90;60;20"
.List = rngSource.Cells.Value
End With
End Sub
This is the makecalendar code:
Sub makeCalendar()
Dim t, lrV, x As Long
Dim StartDay, DayofWeek, CurYear, CurMonth, FinalDay As Date
Dim wsB, ws As Worksheet
Dim MyInput As String
Dim dNum As Range
Set wsB = Sheets("Bills")
With wsB
If IsUserFormLoaded(CalendarOpts.Name) = True Then
MyInput = CalendarOpts.sbMonth.Value & "-" & CalendarOpts.sbYear.Value
Else
MyInput = .Range("cMonth").Value & "-" & .Range("cYear").Value
End If
If MyInput = "" Then Exit Sub
For Each ws In ThisWorkbook.Worksheets
If ws.Name = MyInput Then
ws.Activate
MsgBox ("The Calendar Month you are trying to create already exists." & vbCrLf & " The Options menu will close now.")
End
End If
Next ws
lrV = .Cells(Rows.Count, 2).End(xlUp).Row
End With
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyInput
ActiveSheet.Protect DrawingObjects:=False, Contents:=False, Scenarios:=False
StartDay = DateValue(MyInput)
If Day(StartDay) <> 1 Then
StartDay = DateValue(Month(StartDay) & "/1/" & Year(StartDay))
End If
Range("a1").NumberFormat = "mmmm yyyy"
With Range("a1:h1")
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.Font.Size = 18
.Font.Bold = True
.RowHeight = 35
End With
With Range("a2:h2")
.ColumnWidth = 20
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Orientation = xlHorizontal
.Font.Size = 10
.Font.Bold = True
.RowHeight = 16
End With
Range("a2") = "Sunday"
Range("b2") = "Monday"
Range("c2") = "Tuesday"
Range("d2") = "Wednesday"
Range("e2") = "Thursday"
Range("f2") = "Friday"
Range("g2") = "Saturday"
Range("h2") = "Week Subtotal"
With Range("a3:h8")
.HorizontalAlignment = xlRight
.VerticalAlignment = xlTop
.Font.Size = 12
.Font.Bold = True
.RowHeight = 14
.ColumnWidth = 18
End With
Range("a1").Value = Application.Text(MyInput, "mmmm yyyy")
DayofWeek = Weekday(StartDay)
CurYear = Year(StartDay)
CurMonth = Month(StartDay)
FinalDay = DateSerial(CurYear, CurMonth + 1, 1)
Select Case DayofWeek
Case 1
Range("a3").Value = 1
Case 2
Range("b3").Value = 1
Case 3
Range("c3").Value = 1
Case 4
Range("d3").Value = 1
Case 5
Range("e3").Value = 1
Case 6
Range("f3").Value = 1
Case 7
Range("g3").Value = 1
End Select
For Each dNum In Range("a3:g8")
If dNum.Column = 1 And dNum.Row = 3 Then
ElseIf dNum.Column <> 1 Then
If dNum.Offset(0, -1).Value >= 1 Then
dNum.Value = dNum.Offset(0, -1).Value + 1
If dNum.Value > (FinalDay - StartDay) Then
dNum.Value = ""
Exit For
End If
End If
ElseIf dNum.Row > 3 And dNum.Column = 1 Then
dNum.Value = dNum.Offset(-1, 6).Value + 1
If dNum.Value > (FinalDay - StartDay) Then
dNum.Value = ""
Exit For
End If
End If
Next
For x = 0 To 5
Range("A4").Offset(x * 2, 0).EntireRow.Insert
With Range("A4:H4").Offset(x * 2, 0)
.RowHeight = 85
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = True
.Font.Size = 10
.Font.Bold = False
.Locked = False
End With
With Range("A3").Offset(x * 2, 0).Resize(2, 8)
.Borders(xlLeft).Weight = xlThin
.Borders(xlLeft).ColorIndex = xlAutomatic
.Borders(xlRight).Weight = xlThin
.Borders(xlRight).ColorIndex = xlAutomatic
.BorderAround Weight:=xlThin, ColorIndex:=xlAutomatic
End With
Next x
ActiveWindow.DisplayGridlines = False
End Sub
and this is the cod eto insert the bills:
Sub insertBills_UF()
Dim a, b, r, c, x, i, j As Long
Dim lbCnt As Integer
Dim ws, wsB, wsSel As Worksheet
Dim aBills()
Dim mySheet As String
mySheet = CalendarOpts.lblMonth.Caption & "-" & CalendarOpts.lblYear.Caption
If mySheet = "" Then
MsgBox ("A calendar has not yet been made with the current Month Selection")
Exit Sub
End If
For Each ws In ThisWorkbook.Worksheets
If ws.Name = mySheet Then
ws.Activate
Set wsSel = ws
End If
Next ws
If SheetExist(mySheet) Then
Else
MsgBox ("The Sheet Does NOT Exists")
Exit Sub
End If
lbCnt = CalendarOpts.lbBills.ListCount - 1
ReDim aBills(0 To lbCnt, 0 To 2)
For a = 0 To lbCnt
aBills(a, 0) = CalendarOpts.lbBills.List(a, 0)
aBills(a, 1) = CalendarOpts.lbBills.List(a, 1)
aBills(a, 2) = CalendarOpts.lbBills.List(a, 2)
Next a
With wsSel
For b = 0 To lbCnt
For r = 3 To 38 Step 6
For c = 1 To 7
If .Cells(r, c).Value <> "" Then
If .Cells(r, c).Value = aBills(b, 2) Then
For x = 1 To 5
If .Cells(r + x, c).Value = "" Then
.Cells(r + x, c).Value = aBills(b, 0) & "-" & aBills(b, 1)
Exit For
End If
Next x
End If
End If
Next c
Next r
Next b
End With
End Sub
I have tried adding the modifications to the sheet then re-initialize from the sheet data,
this kind of works. but if there is a way to pull from the modified listbox would be more efficient,
eliminating going back and forth from the sheet and userform.