简体   繁体   中英

Excel VBA Data Validation - Drop Down: Options too many to be saved, causes Error “Excel found unreadable content”

I added a drop down option to a Range of cells on an excel worksheet where you can choose from 100+ accounts loaded from a Database. I added an example code here (the String s contains all accounts, imagine it to be a 500+ character string at least, in the original code the accounts are loaded into the string with a makro):

Dim r As Range
Dim v As Variant
Dim s As String

s = "Account_a,Account_b,Account_c.." ' <-- really long string, contains 100+ accounts
Set r = ActiveSheet.Range("A1:A100")

For Each v In r
    v.Validation.Delete
    v.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
    Operator:=xlBetween, Formula1:=s
Next v

The problem is that Excel is unable to save the Data Validation at a certain String length (for s, Errors already appear at a 256+ char length, after some research i found that this seems to be the breaking point for many excel VBA String functions), so when i save this worksheet, close it and open it again I get an Error ("Excel found unreadable content", and if you try to repair it, excel removes the data validation from the worksheet, since its "not readable"), and the loaded Accounts from the Database are gone.

Is there an option to increase the maximum String length for the data validation when the workbook is saved? The only problem here seems to be the amount of data, since this works fine if i only load 20% of the accounts..

UPDATE: There is an alternative solution in the comments written by AcsErno, which can also be found here: Excel keep loading my spreadsheet with an error (the first answer to the question)

您可以解决将帐户加载到命名范围并将其用作验证列表(如此处)的方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM