【发布时间】:2017-06-04 15:00:34
【问题描述】:
我是 VBA excel 的新手,我在这里尝试使用工作表中的数据填充用户表单中的组合框。我定义了地址并使用了 .rowsource 属性,其中代码通过没有任何错误,但组合框中没有填充任何内容。任何帮助将不胜感激。
下面是代码-
私有子 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
【问题讨论】:
-
代码结束后你真的有
Application.ScreenUpdating = True吗?您至少应该显示到End If -
@Noceo 是的,我最后得到了
Application.ScreenUpdating = True。很抱歉代码不完整,尽管我还有两个 if 列表索引的条件,但我只关心List Index = -1。所以这段代码就可以了,只是缺少一个End If。
标签: vba excel combobox userform