简体   繁体   English

Excel VBA:从commandbutton调用userForm时未显示的ComboBox.Rowsource值的动态范围

[英]Excel VBA: Dynamic range for ComboBox.Rowsource values not displayed when userForm called from commandbutton

The title should give a fair overview of the problem but I'm running a dynamic named range for use in a combo box in a userform. 标题应该给出问题的公平概述,但我正在运行一个动态命名范围,以便在用户窗体的组合框中使用。 When I run the form, the values appear as intended. 当我运行表单时,值显示为预期。 When I call a module sub-routine via a command button, the values don't appear and I've no idea why. 当我通过命令按钮调用模块子例程时,值不会出现,我不知道为什么。

I'll paste all code and highlight the offending snippet(s) below: 我将粘贴所有代码并突出显示以下有问题的代码段:

Private Sub btnGetGAToken_Click()
'--------------------------------
'Obtain API Token from Google Analytics (GA), indicate to user that token has been obtained and populate Account combobox
'with a unique list of accounts, which will in turn populate the Profile combobox with the profiles associated with the chosen
'account
'--------------------------------

Dim txtEmailField As String
Dim txtPasswordField As String

'Values written to sheet for use in UDFToken and UDFGetGAAcctData array formulas
Range("FieldEmail").Value = Me.txtEmailField.Text
Range("FieldPassword").Value = Me.txtPasswordField.Text

Range("GAToken").Calculate

With Me.lblGATokenResponseField
    .Caption = Range("GAToken").Value
    .ForeColor = RGB(2, 80, 0)
End With

Call FindUniqueAccountNames

cboAccountNamesComboBox.RowSource = Sheet1.Range("ListUniqueAccountNames").Address

End Sub

Private Sub cboAccountNamesComboBox_Change()

'Value written to sheet for use in the 'ListProfileNames' dynamic, named range
Range("ChosenAccount").Value = Me.cboAccountNamesComboBox.Value

With Me.cboProfileNamesComboBox
    .Value = ""
    .RowSource = Sheets("CodeMetaData").Range("ListProfileNames").Address
End With

End Sub

The dynamic range was created using the name manager and is below: 动态范围是使用名称管理器创建的,如下所示:

Named Range: "ListUniqueAccountNames" =OFFSET(CodeMetaData!$J$5,0,0,COUNTA(CodeMetaData!$J$5:$J$5000)) 命名范围:“ListUniqueAccountNames”= OFFSET(CodeMetaData!$ J $ 5,0,0,COUNTA(CodeMetaData!$ J $ 5:$ J $ 5000))

and for ease of reference, the code I'm using to run it is below: 为了便于参考,我用来运行它的代码如下:

cboAccountNamesComboBox.RowSource = Sheets("CodeMetaData").Range("ListUniqueAccountNames").Address

The sub-routine calling the userform is here: 调用userform的子例程在这里:

Public Sub ShowReportSpecsForm()

Load frmReportSpecs
frmReportSpecs.Show

End Sub

Forgive me for posting so much of the code, but I'm not sure exactly what it is that's causing the problem - I'm still very much a rookie with forms. 请原谅我发布这么多代码,但我不确定究竟是什么导致了这个问题 - 我仍然是一个有形式的菜鸟。

Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

If you are using the rowsource property and named ranges then I would suggest setting the rowsource property of the combobox's at design time. 如果您正在使用rowsource属性和命名范围,那么我建议在设计时设置组合框的rowsource属性。 Then to debug where required use: 然后调试需要使用的地方:

Debug.Print Range("ListUniqueAccountNames").Address

This will return the named range address to the immediate window where you can check it is correct. 这会将命名范围地址返回到即时窗口,您可以在其中检查它是否正确。

Remember that the property Address from a named dynamic range returns a normal static address. 请记住,命名动态范围中的属性Address返回正常的静态地址。

For example, Range("ListUniqueAccountNames").Address can returns $J$5:$J$20 . 例如, Range(“ListUniqueAccountNames”)。地址可以返回$ J $ 5:$ J $ 20

You do not need use a Excel address in RowSource property. 您不需要在RowSource属性中使用Excel地址。 You can use a Excel name. 您可以使用Excel名称。

Besides, when you show a Userform it is necessary refresh the RowSource property from a ComboBox or ListBox control in order to update its values. 此外,当您显示Userform时,必须从ComboBox或ListBox控件刷新RowSource属性以更新其值。 (Excel control does not watch if the range or data change) (Excel控件不会监视范围或数据是否更改)

That refresh can be made inside Activate event (it runs immediately before form Show and it is shown below) and any situation where data or range changes. 可以在Activate事件内部进行刷新(它在表单Show之前立即运行,如下所示)以及数据或范围发生变化的任何情况。

Private Sub UserForm_Activate()
  Me.cboAccountNamesComboBox.RowSource = ""
  Me.cboAccountNamesComboBox.RowSource = "ListUniqueAccountNames"
End Sub

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

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