gpt4 book ai didi

Excel VBA动态数据验证给出 "Application-defined object"错误

转载 作者:行者123 更新时间:2023-12-04 22:08:42 25 4
gpt4 key购买 nike

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

最佳答案

我做了一些测试,发现(至少在 Excel 2003 中)如果 value_list长度大于 255 个字符,您将收到此错误。

一种可能的解决方法是将列表值加载到命名范围并分配 Formula1:="=YourRangeName" ,但我没有对此进行测试。

关于Excel VBA动态数据验证给出 "Application-defined object"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com