VBA(Visual Basic):ComboBox(表单控件) - 对象不支持此属性或方法

问题描述 投票:2回答:2

刚刚注册! (即使我已经使用该网站几年了哈哈!)。

我在VBA上遇到了问题,它让我发疯了。希望有人可以解决我的问题。

我试图引用我在Sheet6中手动添加的ComboBox(表单控件)。它包含几个项目列表。然后我将循环遍历组合框中的每个项目并用它做一些事情。

然而,对于我的生活,我无法理解我做错了什么?!

我一直得到运行时错误'438':

Object doesn't support this property or method

This is the line the Run-time error occurs on:

Public Sub ReferralSummaries()

'Stop screen updates.
Application.ScreenUpdating = False

'Method Variables
Dim Lstrw As Long
Dim CurrentWb As Workbook
Dim ReferralSheet As Worksheet
Dim ResultsSheet As Worksheet
Dim ReferralSheetCBox As Object

'Initialize Variables
Set CurrentWb = ThisWorkbook
Set ReferralSheet = CurrentWb.Sheets("Referral Breakdowns")
Set ResultsSheet = CurrentWb.Sheets("Results")
Set ReferralSheetCBox = Worksheets(6)

'Check Date column and update accordingly
Call Update_Date_Column(ReferralSheet, ResultsSheet)

For i = 0 To ReferralSheetCBox.ComboBox1.Items.Count - 1 **(ERROR TRIGGERED HERE).**

敬请原谅!

亲切的问候,忍者

vba excel-vba combobox runtime-error excel
2个回答
0
投票

我想你应该用

Set ReferralSheetCBox = Worksheets(6).Shapes("ComboBox1")

然后,您可以通过其ControlFormat属性访问其属性,例如:

Dim nItems As Long
nItems = ReferralSheetCBox.ControlFormat.ListCount

因此,您显示的代码最后一行将是

For i = 0 To ReferralSheetCBox.ControlFormat.ListCount - 1

0
投票

您可以通过强类型ReferralSheetCBox作为DropDown(这是一个包含ComboBox的隐藏类型)来使用早期绑定:

Dim ReferralSheetCBox As Excel.DropDown

然后设置对形状的DrawingObject的引用:

Set ReferralSheetCBox = Sheet6.Shapes("ComboBox1").DrawingObject

然后,您可以将代码调整为:

For i = 0 To ReferralSheetCBox.ListCount - 1
© www.soinside.com 2019 - 2024. All rights reserved.