Data validation - testing for size of list

joey peanuts

New Member
Joined
Mar 20, 2011
Messages
18
Excel 2007

I want a way to test the number of items in a DV list. The list size changes as previous selections are made in other data entry columns.

If there is just 1 item in a particular list, I want to programatically select that item so the user doesn't have to.

Thank you
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I am looking for a function that would return the number of items in a DV list - something like this:


Dim numLISTITEMS as Integer

numLISTITEMS = Range ("A1").validation.count
 
Upvote 0
Maybe like this (minimally tested)
Code:
Function ValListLen(r As Range) As Long
    Dim s As String
 
    On Error GoTo Oops
    With r(1)
        If .Validation.Type = xlValidateList Then
            s = .Validation.Formula1
            If InStr(s, ",") Then
                ValListLen = UBound(Split(s, ",")) + 1
            Else
                ValListLen = Range(s).Cells.Count
            End If
        End If
    End With
Oops:
End Function
 
Upvote 0
Maybe like this (minimally tested)
Code:
Function ValListLen(r As Range) As Long
    Dim s As String
 
    On Error GoTo Oops
    With r(1)
        If .Validation.Type = xlValidateList Then
            s = .Validation.Formula1
            If InStr(s, ",") Then
                ValListLen = UBound(Split(s, ",")) + 1
            Else
                ValListLen = Range(s).Cells.Count
            End If
        End If
    End With
Oops:
End Function

Wow - very creative. If all really want to know is if there is 1 item in the list, could I just declare the ValListLen function as a boolean? Then, use the InStr to look for ",". If comma is found, set ValListLen to false. If comma not found, then set to true (meaning just 1 item).

Thank you
 
Upvote 0
I forgot about the case where there is only a single item in a literal list:
Code:
Function ValListLen(r As Range) As Long
    Dim s As String
 
    On Error GoTo Oops
    With r(1)
        If .Validation.Type = xlValidateList Then
            s = .Validation.Formula1
            If Left(s, 1) = "=" Then
                ValListLen = Range(s).Cells.Count
            Else
                ValListLen = UBound(Split(s, ",")) + 1
            End If
        End If
    End With
 
Oops:
    If Err.Number Then Err.Clear
End Function
For example,
Code:
If ValListLen(Range("A1")) = 1 Then
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top