Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel found unreadable content - Data Validation

Tags:

excel

vba

I have some combo boxes that I populate on opening of the workbook - the source of the data comes from a database.

I populate my combo boxes using data validation with the following code:-

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

where list is a comma separated string that I have built up from the database recordset.

This all works fine. The problem arises when I re-open the workbook later on. I get an error

"Excel found unreadable content. Do you want to recover the contents of this file"

You say Yes and Excel then gives you

"Excel was able to repair the file by removing features"

And the data Validation from some of the Combo boxes is gone

I suspect from some internet searching that the string I'm using for my Data Validation is too long?

It isn't an option for me to add the recordset values to a hidden sheet and set the Data Validation source to a range on the hidden sheet as the combo boxes are dynamic and chop and change depending on user selection. I really just need to be able to set the Data Validation to my string that I have built up at various points in the user interaction.

If it is a case of the string being too long is it possible to append to Data Validation or is there another trick I can use to get around this issue?

like image 927
David Avatar asked Jun 10 '13 11:06

David


2 Answers

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.

like image 131
Baodad Avatar answered Sep 18 '22 07:09

Baodad


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.

like image 24
Wolfgang Kuehn Avatar answered Sep 20 '22 07:09

Wolfgang Kuehn