2017-09-07 7 views
1

を投入 -エクセル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 

これらは私がveifiedているものです -

  1. コンボボックスは、実際の優先順位と呼ばれる、シート1に存在します。 See this screenshot showing that Sheet1 contains a Combobox called ComboBox21

  2. 以下の機能は、LoadDropdown_ClickがSheet1にあります。 See this screenshot for details

  3. このコードは、特定のマシンから実行しているときに機能します。以前はマシン上で動作していましたが、コードや環境を変更せずに突然このエラーが発生しています。

  4. ComboBox21をSheet1.ComboBox21に変更しようとしましたが、コンパイルエラーが発生しました - メソッドまたはデータメンバーが見つかりません。

誰かが助けることができたら嬉しいです!

+1

Sheet1.ComboBox21' –

+1

は、このような状況で "オブジェクトが必要です" '試しは2つのことを示します:1)あなたは'オプションExplicit'を使用していない、そして2) 'ComboBox21 '明らかに範囲内ではないので、@ RobinMackenzieのように予選を試してみてください。また、すべてのモジュールの先頭に 'Option Explicit'を指定し、すべての変数を宣言してください。 –

+1

また、コントロールの名前を 'PrioritySelection'または同様の意味を持つ名前に変更することを強くお勧めします。 'ComboBox21'は何も意味しません。 –

答えて

0

あなたのコードを変更してください:

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 
+0

Sheet1.ComboBox21で置き換えると、元の投稿のポイント4で説明したように、コンパイルエラーが発生します。 - メソッドまたはデータメンバーが見つかりません – Sameer

+0

私はこの変更を行っており、問題なく実行されます。 –

+0

私の元の投稿のポイント3で述べたように、これはいくつかのマシンでうまく動作しますが、問題が発生しているマシンではうまく動作しません。 – Sameer

関連する問題