【问题标题】:Having trouble with syntax for populating listbox填充列表框的语法有问题
【发布时间】:2015-03-20 21:39:17
【问题描述】:

我是一名初级程序员,正在为我的高级顶点项目制作 VBA 宏。我正在尝试使用“A 列”中的数据填充列表框。它必须是动态的,因为用户将编辑数据。我知道对于经验丰富的编码人员来说这很简单,但我遇到了语法问题。任何帮助是极大的赞赏!

Private Sub UserForm_Initialize()

Dim LastRowControllers, LastRowBrakes As Integer
Dim Brakes, Controllers As Range

With Worksheets("ControllersInventory")
LastRowControllers = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With Worksheets("BrakesInventory")
LastRowBrakes = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

Set Controllers = Range(Cells(1, 1), Cells(LastRowControllers, 1))
Set Brakes = Range(Cells(1, 1), Cells(LastRowBrakes, 1))

'Populate Controller_List
Worksheets("ControllerInventory").Select
With Controller_List
.RowSource "= Controllers"
End With

'Populate Brake_List
Worksheets("BrakeInventory").Select

With Brake_List
   .RowSource "= Brakes"
End With
End Sub

我找到了另一种填充列表框的方法,但是我的语法错误,我想通过使用声明的范围来使用正确的编码技术。

Brake_List.RowSource = Worksheets("BrakeInventory").Range(Cells(1, 1), Cells(LastRowControllers, 1)).Address

【问题讨论】:

  • 尝试将.address更改为.value

标签: vba excel


【解决方案1】:

例如:

Dim Rws As Long, Rng As Range
Rws = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(1, 1), Cells(Rws, 1))
ListBox1.List = Rng.Value

【讨论】:

  • @Patrick Lepelletier- 你如何确定范围不是动态的?
  • listbox1.list 以与填充数组相同的方式填充它,以使其动态使用listbox1.rowsource。在更多问题之前,打开 VBE 并尝试...
【解决方案2】:

不用.Select 它不会给你买任何东西

.RowSource 需要一个范围。您正在尝试为其分配一个字符串值(由“”表示),但由于= 符号在引号中,因此您并没有走得那么远。

With Controller_List
  .RowSource = Controllers
End With

'Populate Brake_List
With Brake_List
 .RowSource = Brakes
End With

【讨论】:

  • 这会引发类型不匹配错误,并且无论如何仅适用于活动表中的范围,您需要参考工作表! (见我的回答)
【解决方案3】:

我看到您在使用RangeCells 时经常忘记引用工作表。

Option Explicit


Private Sub UserForm_Initialize()

'first error when declaring variables, you declared some as variant
Dim LastRowControllers as Long, LastRowBrakes As Long 'rows can go more than 65535 (integer limit) , so i use Long
Dim Brakes as Range, Controllers As Range

With Worksheets("ControllersInventory")
    LastRowControllers = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Controllers = .Range(.Cells(1, 1), .Cells(LastRowControllers, 1))
End With

With Worksheets("BrakesInventory")
    LastRowBrakes = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Brakes = .Range(.Cells(1, 1), .Cells(LastRowBrakes, 1))
End With

'Populate Controller_List

With Controller_List
    '.List= Controllers.value 'works but not dynamic
    .Rowsource = Controllers.Parent.Name & "!" & Controllers.Address
End With

'Populate Brake_List
With Brake_List
   '.List = Brakes.value 'works but not dynamic
   .Rowsource = Brakes.Parent.Name & "!" & Brakes.Address ' example  = "Sheet1!$A$1:$A$10"
End With

Set Brakes = Nothing
Set Controllers = Nothing

End Sub

还要注意,如果数据需要是动态的,我认为用户表单必须这样调用:

Userform1.Show False

【讨论】:

    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多