Excel VBA:从命令按钮调用 userForm 时不显示 ComboBox.Rowsource 值的动态范围
标题应该对问题给出一个公平的概述,但我正在运行一个动态命名范围,以便在用户窗体的组合框中使用。当我运行表单时,值按预期显示。当我通过命令按钮调用模块子例程时,这些值不会出现,我不知道为什么。
我将粘贴所有代码并突出显示以下有问题的代码段:
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
动态范围是使用名称管理器创建的,如下所示:
命名范围:“ListUniqueAccountNames” =OFFSET(CodeMetaData!$J$5,0,0,COUNTA( CodeMetaData!$J$5:$J$5000))
为了便于参考,我用来运行它的代码如下:
cboAccountNamesComboBox.RowSource = Sheets("CodeMetaData").Range("ListUniqueAccountNames").Address
调用用户表单的子例程在这里:
Public Sub ShowReportSpecsForm()
Load frmReportSpecs
frmReportSpecs.Show
End Sub
请原谅我发帖这么多的代码,但我不确定到底是什么导致了问题 - 我仍然是表单的新手。
任何帮助将不胜感激。谢谢。
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))
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 rowsource 属性和命名范围,那么我建议在设计时设置组合框的 rowsource 属性。然后在需要的地方进行调试:
这会将命名范围地址返回到立即窗口,您可以在其中检查它是否正确。
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. Then to debug where required use:
This will return the named range address to the immediate window where you can check it is correct.
请记住,命名动态范围中的属性 Address 返回正常的静态地址。
例如,Range("ListUniqueAccountNames").Address 可以返回 $J$5:$J$20。
您不需要在 RowSource 属性中使用 Excel 地址。您可以使用 Excel 名称。
此外,当您显示用户窗体时,必须刷新 ComboBox 或 ListBox 控件中的 RowSource 属性才能更新其值。 (Excel 控件不会监视范围或数据是否发生变化)
可以在 Activate 事件(它在窗体 Show 之前立即运行,如下所示)以及数据或范围发生变化的任何情况下进行刷新。
Remember that the property Address from a named dynamic range returns a normal static address.
For example, Range("ListUniqueAccountNames").Address can returns $J$5:$J$20.
You do not need use a Excel address in RowSource property. You can use a Excel name.
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. (Excel control does not watch if the range or data change)
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.