无法使用.rowsource {vba excel}填充组合框

时间:2017-01-19 19:11:05

标签: excel vba excel-vba combobox userform

我是VBA excel的新手,并尝试使用工作表中的数据填充用户表单中的组合框。我定义了地址和使用.rowsource属性,其中代码通过而没有任何错误,但组合框中没有任何填充。任何帮助将不胜感激。

以下是代码 -

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

新修改代码 -

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

1 个答案:

答案 0 :(得分:0)

编辑:我已经通过回答删除了,因为我没有正确理解这个问题。