ワークブックを開いたときに、ワークシート「データ」のセル「B22」にデータ検証ドロップダウンを取得しようとしています。このエラーが発生します
Run-time error '1004'
Application-defined object error
そして、アプリケーションは次のコードで中断します。
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=value_list
WorkbookOpen() サブは次のとおりです。
Private Sub Workbook_Open()
Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim rng As Range
Set oCon = New ADODB.Connection
oCon.ConnectionString = "Provider=SQLOLEDB;Data Source=MARS;Initial Catalog=automation;Trusted_connection=yes;"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Source = "select insurer.name from person as insurer where insurer.person_class_id = 2 order by insurer.name asc"
oRS.CursorType = adOpenStatic
oRS.Open
value_list = ""
Do While Not oRS.EOF
value_list = value_list & oRS(0) & ","
r = r + 1
oRS.MoveNext
Loop
value_list = Mid(value_list, 1, Len(value_list) - 1)
With Sheets("Data").Range("B22").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=value_list
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
oRS.Close
oCon.Close
End Sub