0

I have a list - that I just found this syntax that will permit multi-selects from my list:

Dim Oldvalue As String
Dim Newvalue As String

On Error GoTo Exitsub
If Target.Address = "$B$1" Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
    If Oldvalue = "" Then
      Target.Value = Newvalue
    Else
      Target.Value = Oldvalue & ", " & Newvalue
    End If
  End If
End If
Application.EnableEvents = True

Exitsub:
Application.EnableEvents = True

I can tell that the List of values selected will be stored in the variable Target.Value - but how do I:

1) Check the length of Target.Value (so I know if I have 1 selected or multi?)

2) Iterate each selection?

3
  • Both of your GoTo Exitsub can be replaced with just Exit. Commented Nov 21, 2016 at 12:55
  • Do you mean you want the length of the string as in a numeric value? Len(target.value) Commented Nov 21, 2016 at 12:58
  • @Andreas - Let's say the List Values are Red, Green, Blue - I need a way of knowing what was selected something like foeach (string pick in Target.Value) msgBox.Show (pick) next Commented Nov 21, 2016 at 12:59

2 Answers 2

2

Without assigning an array you can use

Target.Rows.Count    'number of rows
Target.Columns.Count 'number of columns
Target.Cells.Count   'number of cells

You can loop through them using indices or

Dim cl As Range
For Each cl In Target.Cells 'For Each loops are much faster then looping using indices
    'do something with cl
Next cl

Also note Thomas Inzina's comment that this way you will get all the cells even if you have a discontinuous Range.

Edit: The For Each loop is faster that looping through the cells using indices, i.e.

For i = 1 To Target.Rows.Count
    For j = 1 To Target.Columns.Count
        'do something with Target.Cells(i, j)
    Next j
Next i

Using an array as luke_t suggested might be even faster.

Sign up to request clarification or add additional context in comments.

2 Comments

For each loops are much faster than using indices if you are comparing apples to apple. luke_t is using an array of values, where as you are iterating over cells. My guess is that the array would be faster. However, you have the correct answer. 1) If the Target is a non-contiguous range then only the first area of the range will be copied to the array. 2) It is more intuitive to work with the cells directly as opposed to having to edit an array and copy the values back. +1
@ThomasInzina Yes I should have specified that it's faster than looping through the cells using indices.
1

You need to assign Target.Value to a Variant variable. Remember to include parentheses after the variable name, to denote that you're assigning an Array.

You can then find the dimensions of the array using LBound and UBound, you can also iterate through the array. Pretty sure this is what you're trying to do.

Sub get_vals()

    Dim arr() As Variant
    Dim i As Long

    arr = Range("A1:A5").Value

    Debug.Print UBound(arr, 1) ' Print rows
    Debug.Print UBound(arr, 2) ' Print columns

    For i = LBound(arr, 1) To UBound(arr, 1) ' Iterate through the rows of the array

        Debug.Print arr(i, 1)

    Next i

End Sub

Edit

As raised, you would not be able to assign a one cell range to a Array Variant. You may wish to just use Dim arr As Variant. This will allow you to assign a single cell range to the variable. You can then check the type to determine if you need to iterate an array or just work with a single string/integer.

If TypeName(arr) = "Variant()" Then
    ' Iterate
Else
    ' Work with single string/integer
End If

2 Comments

There is a caveat with this solution: If the target range is only one cell you will get a type mismatch because .Value will return a variant and not a variant array. Depending on the application it might be necessary to check if the range contains multiple cells first.
Very true, @arcadeprecinct. A check would need to be made prior to assigning the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.