Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2004, 04:31 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Array already dimensioned error

Hello All,

I am looping through a range and populating an array which contains a user-defined data type. Now, in that loop I check to make sure the array is not full and if so I redimension it. Please see code sample below:

Sub getIntermediateArray()

    Dim intermediateWkb As Workbook
    Dim intermediateWksht As Worksheet
    Dim intermediateRng As Range
    Dim intermediateRngCounterInt As Integer

    ' Get a handle on the intermediate workbook and worksheet
    Windows(sAggregate).Activate
    Set intermediateWkb = ActiveWorkbook
    Set intermediateWksht = intermediateWkb.ActiveSheet

    intermediateWksht.Range("A1").Select
    intermediateWksht.Range(Selection, Selection.End(xlToRight)).Select
    intermediateWksht.Range(Selection, Selection.End(xlDown)).Select
    Set intermediateRng = Selection

    ' Loop through the range containing the intermediate file
    ' Ignore the first row which contains the column headings ie. start at 2
    For intermediateRngCounterInt = 2 To intermediateRng.Rows.Count
        ' After each pass check the size of the array
        ' If the array size limit has been reached, extend the array size
        If UBound(StockItems) < intermediateRngCounterInt Then
            ReDim Preserve StockItems(UBound(StockItems) + 10)
        End If

        ' Place each row in to a range
        Dim oneStock As Range
        Set oneStock = intermediateRng.Rows(intermediateRngCounterInt)

        ' Query the row range and populate each StockItem in the StockItem array
        StockItems(intermediateRngCounterInt - 1).StockName = oneStock.Cells(1, 1)
        StockItems(intermediateRngCounterInt - 1).AssetClass = oneStock.Cells(1, 3)
        StockItems(intermediateRngCounterInt - 1).Quantity = oneStock.Cells(1, 6)
        StockItems(intermediateRngCounterInt - 1).BankName = oneStock.Cells(1, 13)
    Next intermediateRngCounterInt

    ' Close the intermediate range which we queried to create the StockItem array
    Set intermediateRng = Nothing

End Sub



However, I am receiving an error:
"Compile error: Array already dimensioned"

Any ideas? I only started coding in VBA for the first time last week, so please excuse any stupidness.

Thanks, Selim

 
Old September 22nd, 2004, 08:20 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I managed to figure out the answer for myself in the end.

You cannot ReDim an array whose initial size has been set.

So in order for this to work:
    For intermediateRngCounterInt = 2 To intermediateRng.Rows.Count
        ' After each pass check the size of the array
        ' If the array size limit has been reached, extend the array size
        If intermediateRngCounterInt > UBound(StockItems) Then
            ReDim Preserve StockItems(UBound(StockItems) + 10)
        End If
    Next intermediateRngCounterInt

The StockItems array must be declared as:
    Public StockItems() As StockItem

I previously had it declared as:
    Public StockItems(1 to 100) As StockItem






Similar Threads
Thread Thread Starter Forum Replies Last Post
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
All Array is not returning [Coding Error] vinod_yadav1919 Javascript How-To 0 January 18th, 2005 10:10 AM
Array error in checkout3 file JDBennett BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 January 4th, 2005 06:57 PM
sort array of strings error lanita BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 14th, 2004 08:52 PM
Runtime Error - Is it an array issue? Lucy Classic ASP Professional 1 June 24th, 2004 10:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.