arraylist.ToArray convert to excel.range() problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Enrique Bustamante (ebustamante_at_xstratacoal.com)
Date: 05/06/04


Date: 5 May 2004 17:01:50 -0700

I am creating a function that finds ranges and returns an array with
all ranges found.
I have a problem I have not been able to solve.
The function is below. See the note at the bottom of the procedure.

There is something I am missing here. Please help.

Thank you very much.

 Public Shared Function FindAll(ByVal targetRange As Excel.Range, _
        ByVal What As Object, _
        Optional ByVal LookIn As XlFindLookIn = _
                 XlFindLookIn.xlFormulas, _
        Optional ByVal LookAt As XlLookAt = XlLookAt.xlWhole, _
        Optional ByVal SearchOrder As XlSearchOrder = _
        XlSearchOrder.xlByColumns, _
        Optional ByVal MatchCase As Boolean = False) As Excel.Range()

        'validate range
        If Not targetRange Is Nothing Then

            Dim cell As Excel.Range

            With targetRange

                'create an arraylist to hold the ranges. initial size
                'could be smaller
                Dim cellsFound As ArrayList = New ArrayList _
                    (targetRange.Cells.Count)

                Dim somethingFound As boolean

                     'find first cells
                cell = .Find(What, LookIn:=LookIn, LookAt:=LookAt, _
                        SearchOrder:=SearchOrder, _
                        MatchCase:=MatchCase)

                'if there is at least one cell found look for more
                If Not cell Is Nothing Then

                    somethingFound = true
                 
                    'address to stop when the find method wraps
                    'up to first cell
                    Dim firstAddressFound As String = cell.Address

                    Do
                        
                        'add the cell to the arraylist
                        cellsFound.Add(cell)

                        'find next cell
                        cell = .FindNext(cell)

                        'if we are back to the first cell, exit loop
                    Loop While cell.Address <> firstAddressFound

                    'if cells were found, return the answer,
                    'otherwise return nothing
                    If somethingFound Then

                        'resize the array so there are not empty items
                        cellsFound.TrimToSize()

                        '***here my problem. I get the same exception for all 3
                        'instructions I have tried so far.

                        'exception:
                        'A first chance exception of
                        'type 'System.InvalidCastException'
                        'occurred in OfficeUtilities.exe
                        'Additional information: Specified cast is not valid.

                        'note that: all elements added to the arraylist are
                        'of the same type: excel.range
                        'the arraylist is trimmed so there are not empty nor
                        'undefined items
 
                        Return CType(cellsFound.ToArray(), _
                               Excel.Range())

                        Return DirectCast(cellsFound.ToArray(), _
                               Excel.Range())

                        Return DirectCast(cellsFound.ToArray(GetType _
                               (Excel.Range)), Excel.Range())

                    End If

                End If

            End With

        End If

    End Function



Relevant Pages

  • Re: array Puzzle (spoiler)
    ... >) The key is that the integers in the input array are sequential, ... > So now you know one value, and the sum of the two others. ... some of the missing values is generally applicable. ... None of these ranges can contain *all* of the missing values, ...
    (comp.programming)
  • Re: extract matching vales
    ... If you have dynamic ranges that use the OFFSET function then they won't work ... in other calling workbooks unless the source workbook is open. ... Ref no spaces in list & unique ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)
  • Re: extract matching vales
    ... If you have dynamic ranges that use the OFFSET function then they won't work ... in other calling workbooks unless the source workbook is open. ... Ref no spaces in list & unique ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)
  • Re: extract matching vales
    ... If you have dynamic ranges that use the OFFSET function then they won't ... in other calling workbooks unless the source workbook is open. ... Package Ref does have spaces in list & Duplicates is the same as Ref ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)
  • Re: Series values from an array variable
    ... There are in fact virtual ranges you could use, ... Jon Peltier, Microsoft Excel MVP ... sometimes you will like to plot values smaller than one and some times ... property to the numbers in each array element. ...
    (microsoft.public.excel.programming)