+ Reply to Thread
Results 1 to 7 of 7

Inexplicable Redim error

  1. #1
    Registered User
    Join Date
    05-15-2006
    Posts
    16

    Inexplicable Redim error

    Hi all,

    I have a series of UserForms and at one point I wish to resize an array:
    Redim Preserve ArrayName(newsize)
    however, it gives me a "Subscript out of range" error. But, if I move the resize command to some other forms it works fine (some others not). Is there any logical reason for this? I assumed you could use ReDim at any point.

    If it makes any difference, I'm calling this from within a KeyDown Event, but I'm sure I've successfully done this in the past.

    Thanks for your suggestions,
    Peter.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Peter,

    Something is amiss in your code. Can you post a copy of your workbook?

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    05-15-2006
    Posts
    16
    I can't post the workbook itself as it contains Commercial-In-Confidence data, but here is the relevant code:

    Please Login or Register  to view this content.
    MappingListBox is a combo box on the UserForm.
    MappingList() As String

    Functionality: if text is entered into the combobox (signified by hitting Return) then I check to see if it's a new entry into the list, and if it isn't I add it on and redisplay the new list.

    MappingList is defined before this Form is shown as:

    ReDim Preserve MappingList(1)
    MappingList(1) = "<None>"

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Peter,

    You don't mention or show how you have declared MappingList. It seems to me that you have declared MappingList on some other UserForm. Unless you have declared MappingList as a Public array in the General Declarations of a Standard VBA Module, it won't be available to all the routines in your Project. If you don't have a Standard Module in your project, add one and copy this code into it.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    05-15-2006
    Posts
    16
    Leith (and others),

    Yes, MappingList is a global variable defined in a module as
    Public MappingList() as String

    Attached is a sanitised version of the tool - I have deleted 90% of it, but the front-end where the problem occurs is still there. To get to the error, click on the "Execute RUPERT" button
    - at this stage MappingList is initialised to size 1 and the string "<None>" placed in the first element]

    Next click "Equipment"
    - I don't need to resize MappingList here, but just for demonstration I have and put in msgboxes to show that it works

    Next click "Select Equipment"
    - the exact same process is done here, but the resize fails due to 'Subscript out of range' error!!!

    Not only is this completely baffling to me, but I have an older version which works properly. I have just made a heap of changes after that point that would take me days to rework.

    I'm sure it's something very little and trivial, but I just can't find it and it's driving me insane.

    Thanks again for your help.
    Attached Files Attached Files

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Did you declate Option Base 1 in some of your modules and not in others?

  7. #7
    Registered User
    Join Date
    05-15-2006
    Posts
    16
    Yes, thanks Mik. I usually use Base 1, but in a couple of my modules I needed to use a dummy 0 index. That has fixed the problem.

    Cheers everybody.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1