简体   繁体   English

VBA:ComboBox仅在Workbook_Open事件后显示一项

[英]VBA: ComboBox only showing one item after Workbook_Open event

I am attempting to have a Workbook_Open event populate a controls ComboBox so that when the user goes to the Worksheet("Benchmarking") , they have a pre-populated list to choose from that includes all the items in the array datesArr . 我试图让一个Workbook_Open事件填充一个控件ComboBox,以便当用户转到Worksheet("Benchmarking") ,他们可以选择一个预先填充的列表,该列表包括datesArr数组中的所有项目。

The problem i am having is, upon opening the spreadsheet and navigating to the Worksheet("Benchmarking") , i am only seeing one item in the drop down list: 我遇到的问题是,在打开电子表格并导航到Worksheet("Benchmarking") ,我仅在下拉列表中看到一项:

ComboBox1的现状

If i select that item then the list actually populates: 如果我选择该项目,则该列表实际填充:

选择初始单个项目时出现完整列表

Desired result: 所需结果:

I want the full list to be available from the first time the user tries to make a selection not just after the ComboBox1_Change event is fired. 我希望从用户第一次尝试进行选择起,不仅是在ComboBox1_Change事件被激发之后,才提供完整列表。

Having reviewed numerous post eg Sometimes the ActiveX Combobox only shows one row, why? 回顾了许多帖子,例如, 有时ActiveX组合框仅显示一行,为什么? , Populating Combo Box on WorkBook Open I have tried several different approaches including the following in the Workbook_Open event code: 在WorkBook Open上填充组合框我尝试了几种不同的方法,包括在Workbook_Open事件代码中进行以下操作:

.ListFillRange = "DropDownDates"  
.List = DateArrToStrAr

I have also looped the array adding the items to ComboBox1. 我还循环了将项目添加到ComboBox1的数组。 Each time i get the same 1 visible item in drop down result. 每次我在下拉结果中得到相同的1个可见项目。

Is someone able to tell me where i am going wrong please? 有人可以告诉我我要去哪里了吗?

My current code is 我当前的代码是

1) ThisWorkbook 1)本工作簿

Private Sub Workbook_Open()

    With Worksheets("Benchmarking").OLEObjects("ComboBox1").Object
        .Clear
        .List = DateArrToStrArr '         
    End With

End Sub

2) Worksheet("Benchmarking") : 2) 工作表(“基准测试”)

Private Sub ComboBox1_Change() 'QH 2/11/17

     Dim datesArr() As String
     Dim ws As Worksheet

     Set ws = ThisWorkbook.Worksheets("Lkup")

     datesArr = DateArrToStrArr 'function that reads a named range of dates and converts to string to avoid dd/mm becoming mm/dd

     If ComboBox1.Value = vbNullString Then ComboBox1.Value = "01/04/2016"

     ComboBox1.List = datesArr

     '.....other code

End Sub

Notes: 笔记:

The array datesArr is populated by the function DateArrToStrArr() which reads in a named range of dates "DropDownDates" (workbook scope) and converts them to a string array. datesArr数组由函数DateArrToStrArr()填充,该函数读取指定日期范围的"DropDownDates" (工作簿范围)并将其转换为字符串数组。 This is then assigned to the ComboBox. 然后将其分配给ComboBox。

DropDownDates is a dynamic named range with formula =OFFSET(Lkup!$F$16,,,Lkup!$M$12,) DropDownDates是一个动态命名范围,其公式为=OFFSET(Lkup!$F$16,,,Lkup!$M$12,)

Set-up: Excel 2016 64 bit Windows. 设置:Excel 2016 64位Windows。

Thanks to @CLR for making me think about recalcs. 感谢@CLR让我考虑重新计算。 I decided to hack my way around this with the following: 我决定通过以下方法解决此问题:

I have added in Worksheet("Benchmarking") a Worksheet_Activate event and removed the Workbook_Open code. 我在Worksheet("Benchmarking")了一个Worksheet_Activate事件,并删除了Workbook_Open代码。 This seems to do the trick 这似乎可以解决问题

Private Sub Worksheet_Activate()
  ' ComboBox1.Clear
   ComboBox1.List = DateArrToStrArr
End Sub

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

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