Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 07-19-2016, 07:29 PM
AustinBrister AustinBrister is offline Take String of numbers, expand ranges, sort, then compress back into ranges Windows 7 64bit Take String of numbers, expand ranges, sort, then compress back into ranges Office 2013
Novice
Take String of numbers, expand ranges, sort, then compress back into ranges
 
Join Date: May 2015
Posts: 16
AustinBrister is on a distinguished road
Default

Here's what I have so far:


Code:
Sub ListExpandSortDeDupCompress()

Dim arrItems() As String
Dim OGList As String
Dim SplitList() As String
Dim FinalArray() As Variant
Dim Lease1 As String, Lease2 As String, InLease As Integer
Dim pos As Long

OGList = Selection.Text
OGList = Replace(OGList, " ", "")
SplitList = Split(OGList, ",")

  
Dim i As Integer
Dim j As Integer

For i = LBound(SplitList) To UBound(SplitList)
    If InStr(1, SplitList(i), "-") Then
        pos = CInt(InStr(1, SplitList(i), "-") - 1)
        Lease1 = Left(SplitList(i), pos)
        Lease2 = Right(SplitList(i), Len(SplitList(i)) - pos - 1)
        SplitList(i) = str(Lease1)
        For j = Lease1 + 1 To Lease2
            SplitList(i) = SplitList(i) & "," & str(j)
        Next j
    End If
Next i
  
  
OGList = Join(SplitList, ",")
OGList = Replace(OGList, " ", "")
'MsgBox (OGList)

SplitList = Split(OGList, ",")
Call BubbleSort(SplitList)
OGList = Join(SplitList, ",")
OGList = Replace(OGList, " ", "")
'MsgBox (OGList)

ReDim FinalArray(1 To 1) As Variant
For i = LBound(SplitList) + 1 To UBound(SplitList)
    If Not IsInArray(SplitList(i), FinalArray) Then
    FinalArray(UBound(FinalArray)) = SplitList(i)
    ReDim Preserve FinalArray(1 To UBound(FinalArray) + 1) As Variant
    End If
Next i

OGList = Join(FinalArray, ",")
OGList = Replace(OGList, " ", "")
MsgBox (OGList)



End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function

Sub BubbleSort(arr)
    Dim i As Integer
    Dim j As Integer
    Dim itm As Variant
    Dim Lease1 As String, Lease2 As String, InLease As Integer
    Dim pos As Integer
    
    
    
    
    For i = LBound(arr) To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If InStr(1, arr(j), "-") Then
            pos = InStr(1, arr(j), "-") + 1
            Lease1 = CInt(Left(arr(j), pos))
            Lease2 = CInt(Right(arr(j), Len(arr(j)) - pos))
            End If
            
            If IsNumeric(arr(i)) And IsNumeric(arr(j)) Then
                If Val(arr(i)) > Val(arr(j)) Then
                    itm = arr(i)
                    arr(i) = arr(j)
                    arr(j) = itm
                End If
            ElseIf arr(i) > arr(j) Then
                itm = arr(i)
                arr(i) = arr(j)
                arr(j) = itm
        End If
        Next j
    Next i

End Sub


First of all, THANK YOU SO MUCH!

It looks like I'm really getting somewhere.


The only final step I need to do is to creating the summarized ranges. I think what I need to do there is another loop of some sort that compares sequential entries. If the next entry is one step above the current entry, then the loop (or nested if/then?) would keep going until it finds either the end of the array or an entry that is not sequential. Honestly, this is probably the most daunting task for me. I'm not sure how to structure that loop/whatever.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Take String of numbers, expand ranges, sort, then compress back into ranges Dynamic chart ranges GlowingApple Excel 1 02-17-2016 07:18 PM
Take String of numbers, expand ranges, sort, then compress back into ranges Problem with have a chart with different data ranges expert4knowledge Excel 2 09-09-2014 06:27 AM
Take String of numbers, expand ranges, sort, then compress back into ranges Compare content of two ranges INCLUDING FORMATTING delasson Word VBA 3 12-10-2012 03:54 AM
How to use named ranges in excel vba? bosve73 Excel Programming 4 01-25-2012 09:26 AM
Dynamic Named Ranges using text hannu Excel 0 06-22-2010 04:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2026 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft