Windy borough
New Member
- Joined
- Nov 3, 2018
- Messages
- 35
Hi,
I need some help with my vba code for excel 2016. I have a listbox (named lstData) which displays/fetches data from the workbook sheet. I managed to get Listbox to work and display data but the listbox content doesn't change when combobox selection is made. It shows the data first time but if I change the combobox to another month my listbox doesn't update.
If somebody can point out where I'm entering the code wrong then I'd appreciate it very much.
Combobox named cboMonths is the main which sets the sheet where data goes. Based on number of months there are many sheets. lstData is my listbox which shows only the sheet which is opened in the background and not working on the selected month from the cboMonths. I would like it to show the months data if cboMonths selection is changed regardless of which sheet is opened in excel. Data entry works fine, I can have january 2019 open in the background and I can select February 2019 from cboMonths and data is entered correctly. It's the lstData (listbox) is not working in accordance with cboMonths. Am I entering the wrong vba code? Entering in the wrong section? I'd appreciate it if someone can help me with this.
Cheers.
I need some help with my vba code for excel 2016. I have a listbox (named lstData) which displays/fetches data from the workbook sheet. I managed to get Listbox to work and display data but the listbox content doesn't change when combobox selection is made. It shows the data first time but if I change the combobox to another month my listbox doesn't update.
If somebody can point out where I'm entering the code wrong then I'd appreciate it very much.
Code:
Private Sub UserForm_Initialize()Dim wsActive As Worksheet
Dim i As Long, LastRow As Long
Set wsActive = ActiveSheet
LastRow = wsActive.Cells(wsActive.Rows.Count, "G").End(xlUp).Row
For i = 1 To ThisWorkbook.Sheets.Count
Me.cboMonths.AddItem ThisWorkbook.Sheets(i).Name
Next
'Dim wsActive As Worksheet
'Dim i As Long, LastRow As Long
Set wsActive = ActiveSheet
'LastRow = wsActive.Cells(wsActive.Rows.Count, "G").End(xlUp).Row
'TextBox 1 carries the desired value
'Label8.Caption = " Balance is: " & wsActive.Cells(LastRow, 7).Value
For i = 1 To ThisWorkbook.Sheets.Count
Me.cboMonths.AddItem ThisWorkbook.Sheets(i).Name
Next
Dim Cell As Range
For Each Cell In Range("A8:F8")
cboHeader.AddItem (Cell.Value)
Next Cell
End Sub
Code:
Private Sub cmdGetData_Click()Dim SheetName As String
Dim ws As Worksheet
Dim LastRow As Long
SheetName = cboMonths.Value
Set ws = Sheets(SheetName)
lstData.ColumnCount = 7
lstData.RowSource = "A9:F375"
End Sub
Code:
Private Sub cmdAdd_Click()Dim dcc As Long
Dim abc As Worksheet, pfl As Worksheet
Set abc = ThisWorkbook.Worksheets(Me.cboMonths.Value)
Set pfl = Sheets("ProfitLoss")
With abc
dcc = .Range("A" & Rows.Count).End(xlUp).Row
.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.txtSource.Value
.Cells(dcc + 1, 3).Value = Me.txtRent.Value
.Cells(dcc + 1, 4).Value = Me.txtRentalAdmin.Value
.Cells(dcc + 1, 5).Value = Me.txtMiscHoldingDeposit.Value
.Cells(dcc + 1, 6).Value = Me.txtOut.Value
End With
If CheckBox1.Value Then 'this is a shorter way of writing the conditional
With pfl
dcc = .Range("A" & Rows.Count).End(xlUp).Row
.Cells(dcc + 1, 1).Value = Date
.Cells(dcc + 1, 2).Value = Me.txtSource.Value
.Cells(dcc + 1, 3).Value = Me.txtRent.Value
'.Cells(dcc + 1, 4).Value = Me.TextBox3.Value
'.Cells(dcc + 1, 5).Value = Me.TextBox4.Value
'.Cells(dcc + 1, 6).Value = Me.TextBox5.Value
End With
End If
End Sub
Code:
Private Sub cboMonths_Change()Dim SheetName As String
Dim ws As Worksheet
Dim LastRow As Long
SheetName = cboMonths.Value
Set ws = Sheets(SheetName)
LastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
LabelBalance.Caption = "Balance is: " & ws.Cells(LastRow, 7).Value
End Sub
Combobox named cboMonths is the main which sets the sheet where data goes. Based on number of months there are many sheets. lstData is my listbox which shows only the sheet which is opened in the background and not working on the selected month from the cboMonths. I would like it to show the months data if cboMonths selection is changed regardless of which sheet is opened in excel. Data entry works fine, I can have january 2019 open in the background and I can select February 2019 from cboMonths and data is entered correctly. It's the lstData (listbox) is not working in accordance with cboMonths. Am I entering the wrong vba code? Entering in the wrong section? I'd appreciate it if someone can help me with this.
Cheers.