6

我有一些组合框,我在打开工作簿时填充 - 数据源来自数据库。

我使用以下代码使用数据验证填充我的组合框:-

  With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=list
    .IgnoreBlank = False
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
  End With

其中 list 是我从数据库记录集中构建的逗号分隔字符串。

这一切都很好。稍后我重新打开工作簿时会出现问题。我收到一个错误

“Excel 发现无法读取的内容。是否要恢复此文件的内容”

你说是,然后 Excel 给你

“Excel 能够通过删除功能来修复文件”

并且来自某些组合框的数据验证消失了

我从一些互联网搜索中怀疑我用于数据验证的字符串太长?

将记录集值添加到隐藏表并将数据验证源设置为隐藏表上的范围对我来说不是一个选项,因为组合框是动态的,并且根据用户选择进行切割和更改。我真的只需要能够将数据验证设置为我在用户交互的各个点建立的字符串。

如果是字符串太长的情况,是否可以附加到数据验证,或者我可以使用另一个技巧来解决这个问题?

4

5 回答 5

5

I've manipulated validation lists before in some of my Excel projects. When you set validation to Allow:List, you can set your data Source to be a workbook-level named range. In this example, I've defined a named range "listrange":

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=listrange"
    .IgnoreBlank = True
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
End With

You'll never get an error for that formula string being too long.

I put all my validation-referenced named ranges in one worksheet, and make it hidden. Then my code manipulates those named ranges, which in turn update the values available from the validation drop-down menus.

It can be tricky to dynamically update the size of the named ranges while they are being updated, but it's not too hard with VBA, particularly not if you're returning sets from a database, where you can get a record count. The alternative is to go the ActiveX control route, but I like the clean, native look and feel of the data validation drop-downs.

于 2013-09-23T19:36:21.160 回答
2

我通过删除 WorkbookBeforeSave 事件中的验证解决了这个问题。但是,我正在使用 C#

于 2015-01-15T13:30:25.760 回答
2

Just ran into this issue (limit on data validation formula length at workbook opening), and like the OP wouldn't want to go with auxiliary ranges.

My workaround is to delete the validations in the Workbook_BeforeSave handler.

My use case is to always refresh the data from external sources, so it is a viable option to always delete all imported data and validations before saving the workbook.

于 2014-06-16T12:02:45.460 回答
1

看来您对字符串长度的判断是正确的Validation formula1 parameter。我对您的建议如下(附加信息在代码中作为注释放置):

'Split your list into array, or if data are Array before you _
create List variable you could combine some of earlier steps _
of your code

    List = Split(List, ",")
'paste your list into hidden sheet as of A1 direction bottom, _
we need to transpose our Array to do so
    Sheets("hidden").Range("a1").Resize(UBound(List) + 1, 1) = Application.Transpose(List)

 With Selection.Validation
    .Delete
    'here we need to change definition of formula
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
    Formula1:="=Hidden!A1:A" & UBound(List) + 1
    .IgnoreBlank = False
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
  End With
于 2013-06-10T12:01:18.063 回答
0

有一种解决方法,在使用之前将用于条件格式的字符串保存在工作簿的某个位置。当你使用它们时,将它们引用到你保存它们的范围,而不是从字符串中。请记住,条件格式中的 longs 字符串是导致它执行一个功能的原因,该功能在关闭工作簿时会消除有问题的条件格式,而另一个功能会在打开时将它们重新打开

问题解决了,它很有效:)

于 2014-07-05T07:08:38.980 回答