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
and so on.
However, in the real world, these numbers are in a column on a sheet, and when i use:
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:
The above example works fine, and outputs : 1-3, 6-8, 14-16, 22
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.