填充ComboBox时Excel VBA对象所需的错误
我收到以下错误 –
运行时错误“424”:需要的对象
这里是我得到错误信息的代码。 出现错误的行用****突出显示
Sub LoadDropdown_Click() Dim cnt As ADODB.Connection Dim rst As ADODB.Recordset Dim stDB As String, stSQL As String Dim xlCalc As XlCalculation Dim vaData As Variant Dim k As Long Set cnt = New ADODB.Connection cnt.connectionString = Module1.GetConnectionString stSQL = "EXEC dbo.GetData" With cnt .CursorLocation = adUseClient 'Necesary for creating disconnected recordset. .Open stConn 'Open connection. 'Instantiate the Recordsetobject and execute the SQL-state. Set rst = .Execute(stSQL) End With With rst Set .ActiveConnection = Nothing 'Disconnect the recordset. k = .Fields.Count 'Populate the array with the whole recordset. vaData = .GetRows End With 'Close the connection. cnt.Close 'Manipulate the Listbox's properties and show the form. With ComboBox21 .Clear ' **** the error comes at this line since ComboBox21 is empty ****** .BoundColumn = k .List = Application.Transpose(vaData) .ListIndex = -1 End With Dim i As Integer 'Release objects from memory. Set rst = Nothing Set cnt = Nothing End Sub
这些是我已经实现的东西 –
-
combobox实际上存在于Sheet1中,称为优先级。 显示Sheet1包含名为ComboBox21的Combobox
-
下面的函数LoadDropdown_Click存在于Sheet1中。 详细信息请参见此屏幕截图
-
此代码在从某些机器运行时起作用。 它曾经在我的机器上工作,但现在我突然得到这个错误,没有做任何改变的代码或环境。
-
我试图改变ComboBox21 Sheet1.ComboBox21,但我有一个编译错误 – 找不到方法或数据成员。
如果有人能帮助,这将是非常好的!
请更改您的代码:
With ComboBox21 .Clear ' **** the error comes at this line since ComboBox21 is empty ****** .BoundColumn = k .List = Application.Transpose(vaData) .ListIndex = -1 End With 'With the below: Sheet1.ComboBox21.Clear With Sheet1.ComboBox21 .BoundColumn = k .List = Application.Transpose(vaData) .ListIndex = -1 End With