简体   繁体   English

如何在VBA中引用控件

[英]How to reference a Control in VBA

I'm having trouble with the following code - 我在使用以下代码时遇到麻烦-

Sub ListBox7_Change()
    With Courses.ListBox7
        .AddItem "Hi"
    End With
End Sub

Whenever I run it, I get an error message that says "Run-time error '424': Object required". 每当我运行它时,我都会收到一条错误消息,指出“运行时错误'424':必需对象”。

The debugger highlights the 2nd part of the second line, "Courses.ListBox7". 调试器突出显示第二行的第二部分“ Courses.ListBox7”。

I have a Sheet calles "Courses" and a ListBox on that Sheet called ListBox7. 我有一个工作表称为“课程”,并且在该工作表上有一个列表框,称为ListBox7。 Am I referencing that ListBox incorrectly? 我是否错误地引用了ListBox? What am I doing wrong? 我究竟做错了什么?

I am (unfortunately) using Excel 2010. 我(很不幸)正在使用Excel 2010。

Many thanks, Sam. 非常感谢,山姆。

It's correct as long as Courses is CodeName of your sheet but I don't think so. 只要“ Courses是工作表的CodeName就可以了,但是我不这么认为。

As long as Courses is simple name which you could see in Excel App then you need to change your code into this one: 只要“ Courses是简单名称(您可以在Excel App中看到),就需要将代码更改为以下代码:

Sub ListBox7_Change()
    With Sheets("Courses").ListBox7
        .AddItem "Hi"
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM