当工作簿打开时,我试图将数据验证下拉到工作表“数据”中的单元格“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