I'am a newbie in VBA excel and trying here to populate a combobox in a userform with data from a sheet. I defined address & used the .rowsource property wherein code go through without any error but nothing populates on the combobox. Any help will be appreciated.
Below is the code -
Private Sub empSearchdb_Click()
Dim cell As Range
Dim emptyRow As Long
Dim rng As Range
Dim ws1 As Worksheet
Dim empSearchKeyword As String
'Stop screen flickering
Application.ScreenUpdating = False
'setting worksheet
Set ws1 = Sheets("EmployeeDetails")
ws1.Activate
'find last empty row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2
'when nothing is selected
If Me.empSearchL.ListIndex = -1 Then
'from $A$1 to $J$lastrow-1
Set rng = Range("$A$1", Cells(emptyRow - 1, 10))
With Me.empDetails
.RowSource = rng.Address
End With
New modified code -
Dim cell As Range
Dim emptyRow As Long
Dim rng As Range
Dim ws1 As Worksheet
Dim empSearchKeyword As String
'Stop screen flickering
Application.ScreenUpdating = False
'setting worksheet
Set ws1 = ActiveWorkbook.Sheets("EmployeeDetails")
'find last empty row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2
'from $A$1 to $J$lastrow-1
Set rng = ws1.Range("$A$1", ws1.Cells(emptyRow - 1, 10))
Set rng = ws1.Range("$A$1", "$J$10")
'With your listbox do the following
If Me.empSearchL.ListIndex = -1 Then
With Me.empDetails
'If nothing is selected, add a row source
.ColumnCount = 10 'Otherwise you will only get 1 column
.RowSource = rng.Address
End With
End if
Application.ScreenUpdating = True
Application.ScreenUpdating = True
after the code ends? You should at least show untilEnd If
– Jakob Busk SørensenApplication.ScreenUpdating = True
at the end. Sorry for incomplete code though I have two more if conditions for list indexes but I am concerned about onlyList Index = -1
. So this code will do, just anEnd If
missing. – jay