使用工作表中的数据填充列表框
目前,当我运行代码时,我收到以下错误(如下所示)。
Type Mismatch
为了提供一些上下文,工作表 CourseSelection 的第 3 行从 A 到 F 填充。我想将 A2:A6 中的条目放入列表框中。但是,我想概括此过程,并使其动态地包含在 F 列之后添加的其他类别。因此,我需要一种自动方法来通过类似于下面的代码来执行此操作。但是,我收到错误消息,但不确定原因。
我在此代码之前将 TaskList 定义为 Range。当我运行代码时将鼠标悬停在 xlToRight 上时,我看到一个非常大的负值 (-4191)
。我不确定这是否是问题的一部分。
With Worksheets(CourseSelection).Range("A3")
Set TaskList = Range(.Offset(0, 1), .End(xlToRight))
End With
frmTaskSelection.lbTasks.RowSource = TaskList
I currently get the following error when I run the code (shown below)
Type Mismatch
To give a bit of context, the worksheet CourseSelection has row 3 populated from A to F. I would like to put the entries from A2:A6 into a listbox. However, I want to generalize this process and make it dynamic to include additional categories if they are added after column F. Therefore I need an automatic way to do this through code similar to what I have below. However, I am getting error messages and I am unsure why.
I defined TaskList as a Range prior to this code. When I hover over xlToRight when I run the code I see a very large negative value (-4191)
. I am unsure if this is part of the problem.
With Worksheets(CourseSelection).Range("A3")
Set TaskList = Range(.Offset(0, 1), .End(xlToRight))
End With
frmTaskSelection.lbTasks.RowSource = TaskList
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您将 CourseSelection 定义为返回现有工作表名称的常量,否则代码将在
With Worksheets(CourseSelection).Range("A3")
上失败。如果您想使用工作表名称 CourseSelection,则可以使用With Worksheets("CourseSelection").Range("A3")
。鉴于您的错误消息,尽管您似乎已经过了这一点,并且您的代码似乎在
frmTaskSelection.lbTasks.RowSource = TaskList
上失败。这是因为 RowSource 需要地址。如果您希望将名为 CourseSelection 的工作表中的值从 A3 填充到 Ax,其中 x 是最后使用的单元格,则此代码将在任何活动工作表中运行。
请注意,我不清楚除 A2:A6 之外您还想如何使用 F 列中的其他值。如果您可以提供进一步的指导/图片等,那么可以调整下面的代码以适应
Unless you have CourseSelection defined as a constant returning an existing worksheet name then the code will fail on
With Worksheets(CourseSelection).Range("A3")
. If you want to work with a sheet name CourseSelection you would useWith Worksheets("CourseSelection").Range("A3")
.Given you error message though you appear to have gotten past this point and your code appears to be failing on
frmTaskSelection.lbTasks.RowSource = TaskList
. This is because RowSource expects an addressIf you were looking to populate the values from a sheet called CourseSelection from A3 to Ax where x is the last used cell, then this code will work from any active sheet.
Please note thate I was unclear as to how you wanted to use further values from column F in addition to A2:A6. If you can provide further guidance/picture etc then the code below can be adapted to suit