+ Reply to Thread
Results 1 to 7 of 7

Problem comparing items in (2-dim?) array created from range

  1. #1
    Registered User
    Join Date
    09-14-2009
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2007
    Posts
    3

    Lightbulb Problem comparing items in (2-dim?) array created from range

    Hi all. I've made a Sub that goes through a list of sorted numbers, and checks for continous ranges, like this: 1, 2, 3, 4, 5, 6
    I'd like to collapse these ranges to this format: 1-6.
    So i check if the next number is one bigger than the previous number. If it's not, I build a string of the first and last number of a continous range, with a dash in the middle, and a comma and a space at the end, like this: 1-6,
    Then i write the next number to a temporary string and repeats.
    I got this working good, when I hardcoded an array
    Please Login or Register  to view this content.
    and so on.

    However, in the real world, these numbers are in a column on a sheet, and when i use:
    Please Login or Register  to view this content.
    a twodimensional array seems to be created, so i have to access it's items by writing MyArray(i, 1). When doing this I get a problem when trying to compare the value of one item in the array with the previous.

    Here follows the code for the harcoded version, and then the one where the array is populated from a range:

    Please Login or Register  to view this content.
    The above example works fine, and outputs : 1-3, 6-8, 14-16, 22

    Please Login or Register  to view this content.
    This I can't get working. Something I have not understood about arrays I guess.
    I left my Excel VBA Power Programming book at work, and is trying to work this out from my home office, so any help is appreciated.
    My plan, after getting the comparison thing working, is to generalize this further and make a function so it can be used in and excel cell by anyone at the office.

    Hope anyone see the obvious mistake I'm making here and points it out to me

    Many thanks in advance.

    Regards,

    Tom

  2. #2
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Problem comparing items in (2-dim?) array created from range

    Hi Tom

    I am not really following your post. Nevertheless, based on your first example
    I put the numbers 1,2,3,6,7,8,14,15,16,22 into column E and used

    MyArray = Range("E2:E11").Value ( versus your code in the 2nd example )
    MyArray = Range("E2:E17").Value

    and the output from the 2nd routine was the same as the output from the
    1st routine.

    That isdebugger)

    1-3, 6-8, 14-16, 22


    FWIW
    regards
    John

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Problem comparing items in (2-dim?) array created from range

    I amended the range to end at E11 to fit with your first example and the second bit of code worked for me.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Problem comparing items in (2-dim?) array created from range

    or

    Please Login or Register  to view this content.



  5. #5
    Registered User
    Join Date
    09-14-2009
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Problem comparing items in (2-dim?) array created from range

    Hi people.
    It did work, when i formatted the cells to Number format.
    The range was a product of a len() and right() funtion to strip some text, and that left the cell formatted as General, with the green triangle.
    Thanks for testing and confirming, I was certain there was something wrong with the routine.

    snb: I will try to read and understand your code some time during the day.
    It took me some thinking and trial to make this, my first real function ever, so i guess it's not the most beautiful

    Good guys, fine forum.

  6. #6
    Registered User
    Join Date
    09-14-2009
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Problem comparing items in (2-dim?) array created from range

    Actually, it didn't work inn all cases. Figured I had to test it some, and when one number not part of any continous range appeared in the middle somewhere, it caused problems.
    So I figured i had to put in another test, and an incrementor to check how many times the if-statement had run. Anyways, here's the code for collapsing a range of numbers!
    Someone in the office is going to save lots of time now, thanks to my persistence.
    Worked a long time trying to learn to program, but now it feels like I'm beginning to think right. Please, feel free to point out any bad habits in this example, as I'm all fresh but genuinely interested and willing to learn.

    Please Login or Register  to view this content.
    I guess next step is to make this sub into a function, that lets me type =Collapse in a cell, and then lets me select a range. Then write the output to a cell.

    Here are some keywords for people trying to search the internet for this type of code, as I did:
    collapse continous range boundaries function excel vba

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Problem comparing items in (2-dim?) array created from range

    Thanks for posting your solution. Looks good. You've done the hard work, turning it into a function will be the easy bit now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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