我在Excel 2010中使用VBA填充SQL数据库中的数据。
我将Dropdown添加到某些列以便于编辑。下拉列表中包含来自sql表的数据。
我正在生成这样的Dropdown:
Sub AddLookupToRange(f As tField, r As Range)
If Not f.IsLookup Then
Exit Sub
End If
Dim elems As String
Dim k As Long
elems = ""
For k = 0 To f.Lookup.Elements.count - 1
elems = elems & f.Lookup.Elements.Items(k) & ","
Next k
elems = Left(elems, Len(elems) - 1)
With r.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlEqual _
, Formula1:=elems
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
编辑:
Public Type tLookup
LookupTable As String
SaveValue As Boolean
IndexField As String
ElementsField As String
LutFilter As String
Elements As Scripting.Dictionary
End Type
Public Type tField
Name As String
ReadOnly As Boolean
IsLookup As Boolean
Lookup As tLookup
IsMandatory As Boolean
End Type
现在问题:一切正常,直到我保存并关闭xlsm文件。 当我重新打开它时,文件中有不可读的部分以及我是否要修复它。在修复时,将打开一个窗口,其中显示以下消息:"已删除不可读部分。删除的功能:/xl/worksheets/sheet1.xml-Part"中的数据验证; (翻译自德语)。
看起来Excel无法处理下拉的那种用法。我该如何解决它?
编辑:如果我用文字字符串填充下拉条目(" 1,2,3"),则不会发生错误。如果我这样做,我可以保存并加载文件而不会出错。所有跌落都恢复了。进一步的研究表明,如果字符串' elems'超过254个字符。结论:下拉可以在操作中处理更长的字符串,但在保存/恢复时限制为254个字符。