简体   繁体   English

VBA:在工作表而非用户窗体上填充ListBox ActiveX控件

[英]VBA: Populating ListBox ActiveX control, on worksheet and not user form

I'm trying to populate a List Box (ActiveX control). 我正在尝试填充列表框(ActiveX控件)。 This list box is on a sheet labeled "Dashboard" of my workbook, and not a user form. 此列表框位于我的工作簿中标有“仪表板”的工作表上,而不是用户窗体上。 I want to populate it with a range from a sheet labeled "Data". 我想用标签为“数据”的工作表中的范围填充它。

My problem is that if I populate it on the workbook open event procedure, I get an error when the workbook opens that it "Can't execute in break mode." 我的问题是,如果我在工作簿打开事件过程中填充它,则在工作簿打开时会收到错误消息,提示“无法在中断模式下执行”。 However, there aren't breakpoints active at all. 但是,根本没有活动的断点。

If I populate it on the "Dashboard" worksheet active event procedure, it won't populate when the workbook is open. 如果我在“仪表板”工作表活动事件过程中填充它,则在打开工作簿时将不会填充它。 It only populates if I click on another worksheet, and then go back to the Dashboard worksheet, then it will populate. 仅当我单击另一个工作表,然后返回到“仪表板”工作表时,它才会填充。

Is there a better way to populate the list box so that it's always populated and ready to go? 有没有更好的方法来填充列表框,使其始终填充并准备就绪? I have a lot of vLookup functions that are associated with the list box, and if the list box is not populated, then the rest of my code won't work. 我有很多与列表框关联的vLookup函数,如果未填充列表框,则其余代码将无法工作。

I will post my codes that I have so far. 我将发布到目前为止的代码。 The first is when I attempt to populate the listbox through the workbook_open even procedure. 第一个是当我尝试通过workbook_open even过程填充列表框时。 The second is through the "Dashboard" worksheet activate procedure. 第二个是通过“仪表板”工作表激活过程。

Private Sub Workbook_Open()
Dim strName As String
Dim blDone As Boolean
Dim cPlanets As MSForms.ListBox
Dim vArray As Variant
Dim shtData As Worksheet
Dim wkbSolarSystem As Workbook
Set wkbSolarSystem = Application.Workbooks("workbookname.xlsm")
Set shtData = wkbSolarSystem.Worksheets("Data")
Set cPlanets = wkbSolarSystem.Worksheets("Dashboard").lstPlanets

vArray = shtData.Range("Planets").value
cPlanets.List = vArray
cPlanets.ListIndex = 3


'input box message for user when workbook opens up
strName = InputBox("Hello! Please enter your name", "Welcome!")

'check if there is a name entered via loop and if statement
Do
    If Len(strName) = 0 Then
        'if no name entered, ask user again
        MsgBox ("Please enter a valid name to continue"), vbCritical, "Valid Name Required"
        'ask user to type in name again
        strName = InputBox("Hello! Please enter your name", "Welcome!")
    Else
        'display message with information for user
        MsgBox ("Hello, " & strName)
        blDone = True
    End If
'finish loop statement
Loop Until blDone = True

This next code is the one I have on Sheet3 code worksheet activate procedure 接下来的代码是我在Sheet3代码工作表激活过程中得到的代码

Private Sub Worksheet_Activate()
Dim shtData As Worksheet
Dim wkbSolarSystem As Workbook
Set wkbSolarSystem = Application.Workbooks("workbookname.xlsm")
Set shtData = wkbSolarSystem.Worksheets("Data")

lstPlanets.List = shtData.Range("Planets").value
lstPlanets.ListIndex = 3


End Sub

You are declaring cPlanets as MSForms.ListBox, but in your question you say you are working with an ActiveX listbox on a sheet. 您将cPlanets声明为MSForms.ListBox,但是在您的问题中,您说您正在使用工作表上的ActiveX列表框。 So you should declare cPlanets as an Object, like: 因此,您应该将cPlanets声明为一个对象,例如:

Dim cPlanets As Object
Set cPlanets = wkbSolarSystem.Worksheets("Dashboard").lstPlanets

I tried this code on a different computer, and it works. 我在另一台计算机上尝试了此代码,并且可以正常工作。 It seems that it's the computer configuration that was causing the issue. 似乎是导致此问题的计算机配置。 The code works fine as it should on several different computers. 该代码在多台不同的计算机上都可以正常工作。

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

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