+ Reply to Thread
Results 1 to 5 of 5

Subscript out of range - ReDim Preserve an array

  1. #1
    Registered User
    Join Date
    10-05-2013
    Location
    Moscow, Russia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Subscript out of range - ReDim Preserve an array

    Hi!

    Please help me solve the following problem...

    I'm trying to copy a range from the worksheet into a VBA array. This range is basically a column of zeroes and ones. Then I want to perform operations on this array: to increment the counter if an array element is 1 and to decrement if it is 0. Then I need to pass all the intermediary positions of the counter into another array. Which I would copy into another range on the worksheet.

    If I only increment the counter, everything goes fine. But when I try to decrease it, I get a "subscript out of range" error. I guess it happens because I have a negative number in the ReDim Preserve statement.

    I'm a VBA newbie and can't figure out the solution for this.

    The following code gives me a "Subscript of Range" error.


    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Subscript out of range - ReDim Preserve an array

    you cant do it that way, you have not defined i so it is 0. You will need to error trap the fact that new array doesnt exist with any data in.

    if you have a 0 in A1, you are redimming the array as being -1 in size.

    If element = 0 Then i=iif(i=0,0,i-1)
    Last edited by nathansav; 05-12-2014 at 11:19 AM.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    10-05-2013
    Location
    Moscow, Russia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Subscript out of range - ReDim Preserve an array

    Quote Originally Posted by nathansav View Post
    you cant do it that way, you have not defined i so it is 0. You will need to error trap the fact that new array doesnt exist with any data in.

    if you have a 0 in A1, you are redimming the array as being -1 in size.

    If element = 0 Then i=iif(i=0,0,i-1)
    Nathansav, thank you very much for your answer. Didn't know about "IIf". This resolved the "subscript out of range".
    However I understood I need to reformulate the question.

    I realized I actually need negative numbers to appear in the resulting array.
    I need to monitor all the iterations of the counter and if they are negative, or zero, I need them to display too.
    If, for example, in the source column I start from zero (A2=0) , and I have three zeroes following it (A3=0; A4=0; A5=0) I want the following to display in the resulting column:
    -1
    -2
    -3


    Can this be done somehow with ReDim Preserve or any other method?

    Best regards,
    dejavision.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Subscript out of range - ReDim Preserve an array

    Yes it can, you'll need to have a counter for the array size as well as i though, so have iCounter, set it to 0 at first, then Redim Preserve Array(iCOunter) then Array(iCounter)=i and add iCounter=iCounter+1

  5. #5
    Registered User
    Join Date
    10-05-2013
    Location
    Moscow, Russia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Subscript out of range - ReDim Preserve an array

    Quote Originally Posted by nathansav View Post
    Yes it can, you'll need to have a counter for the array size as well as i though, so have iCounter, set it to 0 at first, then Redim Preserve Array(iCOunter) then Array(iCounter)=i and add iCounter=iCounter+1
    Thanks a lot, Nathansav, it's perfect. My problem is solved. Here is the final code:

    Please Login or Register  to view this content.
    Best regards,
    dejavision.
    Last edited by dejavision; 05-13-2014 at 04:36 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Redim Preserve array: subscript out of range
    By supern0va in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-02-2012, 12:58 PM
  2. redim preserve multidimensional array
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2011, 02:48 PM
  3. redim array error....subscript out of range
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2011, 11:07 AM
  4. dynamic array redim preserve: run-time error 9, Subscript out of range.
    By Broesel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2009, 08:39 AM
  5. Redim Preserve 2D Array
    By kraljb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2006, 03:23 PM

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