Consulting

Results 1 to 7 of 7

Thread: Array Problem

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Array Problem

    I'm trying to set an array as follows

    Dim MyExt(), s
    MyExt = Range("A32", Cells(65536, 1).Offset.End(xlUp)).Value
        For Each s In MyExt
            Debug.Print s
        Next

    This works fine as long as there is more than one cell in the range, but falls down if one cell only is found. MyExt is passed to another function for subsequent processing.
    Any suggestions? I don't really want to loop through the range of cells, redim the array etc.
    MD

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    How about a little error handling?

    On Error Resume Next
    If Err <> 0 Then GoTo whereverHereAfterNext
        'Other code
    End If
    Next

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Can you check to see if the value is 1 like this?

    If MyExt = 1 Then
    'Do Something
    Else
    For Each s In MyExt 
    Debug.Print s 
    Next 
    End If

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Why do you want an array variable? For large ranges there can be performance benefits but, that aside, the range is an array of cells with a default property of .Value. Could you not just pass the range and then it wouldn't matter how many cells were in it?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hey MD,

    You could also do something like

    If Range("A65536").End(xlUp).Row < 33 Then
     '...
    Else
     MyExt = Range("A32", Cells(65536, 1).Offset.End(xlUp)).Value
     For Each s In MyExt
      Debug.Print s
     Next
    End If
    Matt

  6. #6
    VBAX Regular
    Joined
    Jun 2004
    Location
    USA
    Posts
    10
    Hi,
    Range.Value returns:
    - an array of variant if several cells,
    - but a single value of the type of the data in the cell if only a single cell.
    (you can see it by displaying Typename(rg.value))
    Therefore trying to assign a single value to a full array fails.
    In this case, force the assignment to an array element and not the full array:

    Dim MyExt(), s
    Dim rg As Range, rg2 As Range
    Set rg = Range("a32")
    Set rg2 = Range(rg, rg.EntireColumn.Cells(65536).End(xlUp))
    If rg2.Cells.Count = 1 Then
       ReDim MyExt(0 To 0)
       MyExt(0) = rg2.Value
    Else
       MyExt = Range(rg, rg.EntireColumn.Cells(65536).End(xlUp)).Value
    End If
    For Each s In MyExt
        Debug.Print ">" & s
    Next
    Regards,
    Sebastien
    Seb

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks all.
    Sebastien's code fitted by need exactly!
    MD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •