是否可以在 excel 验证下拉框中增加 256 个字符的限制?

Is it possible to increase the 256 character limit in excel validation drop down boxes?

提问人: 提问时间:9/18/2008 最后编辑:Unheilig 更新时间:12/22/2014 访问量:7944

问:

我正在动态创建验证,并达到了 256 个字符的限制。我的验证如下所示:

Level 1, Level 2, Level 3, Level 4.....

除了指向范围之外,还有什么方法可以绕过字符限制吗?

验证已在 VBA 中生成。增加限制是避免对工作表当前工作方式产生任何影响的最简单方法。

Excel VBA

评论


答:

6赞 Graham 9/20/2008 #1

我很确定没有办法绕过 256 个字符的限制,Joel Spolsky 在这里解释了原因:http://www.joelonsoftware.com/printerFriendly/articles/fog0000000319.html

但是,您可以使用 VBA 通过对 Worksheet_Change 事件进行编码来接近复制内置验证的功能。这里有一个模型来给你这个想法。您可能希望重构它以缓存 ValidValues、处理对单元格范围的更改等......

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ValidationRange As Excel.Range
Dim ValidValues(1 To 100) As String
Dim Index As Integer
Dim Valid As Boolean
Dim Msg As String
Dim WhatToDo As VbMsgBoxResult

    'Initialise ValidationRange
    Set ValidationRange = Sheet1.Range("A:A")

    ' Check if change is in a cell we need to validate
    If Not Intersect(Target, ValidationRange) Is Nothing Then

        ' Populate ValidValues array
        For Index = 1 To 100
            ValidValues(Index) = "Level " & Index
        Next

        ' do the validation, permit blank values
        If IsEmpty(Target) Then
            Valid = True
        Else
            Valid = False
            For Index = 1 To 100
                If Target.Value = ValidValues(Index) Then
                    ' found match to valid value
                    Valid = True
                    Exit For
                End If
            Next
        End If

        If Not Valid Then

            Target.Select

            ' tell user value isn't valid
            Msg = _
                "The value you entered is not valid" & vbCrLf & vbCrLf & _
                "A user has restricted values that can be entered into this cell."

            WhatToDo = MsgBox(Msg, vbRetryCancel + vbCritical, "Microsoft Excel")

            Target.Value = ""

            If WhatToDo = vbRetry Then
                Application.SendKeys "{F2}"
            End If

        End If

    End If

End Sub