Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 07-19-2016, 09:02 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

I found this function. However, it isn't doing anything to my string. Is that because I need to convert my string to a long/integer array ?




Code:
Public Function RangeExtraction(AList) As String
'AList is a variant that is an array, assumed filled with numbers in ascending order
Const RangeDelim = "-"          'range delimiter
Dim result As String
Dim InRange As Boolean
Dim Posn, i, ub, lb, rangestart, rangelen As Integer
 
result = ""
'find dimensions of AList
ub = UBound(AList)
lb = LBound(AList)
Posn = lb
While Posn < ub
  rangestart = Posn
  rangelen = 0
  InRange = True
  'try to extend the range
  While InRange
    rangelen = rangelen + 1
    If Posn = ub Then
      InRange = False
    Else
      InRange = (AList(Posn + 1) = AList(Posn) + 1)
      Posn = Posn + 1
    End If
  Wend
  If rangelen > 2 Then 'output the range if it has more than 2 elements
    result = result & "," & Format$(AList(rangestart)) & RangeDelim & Format$(AList(rangestart + rangelen - 1))
  Else 'output the separate elements
    For i = rangestart To rangestart + rangelen - 1
      result = result & "," & Format$(AList(i))
    Next
  End If
  Posn = rangestart + rangelen
Wend
RangeExtraction = Mid$(result, 2) 'get rid of first comma!
End Function
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 06:18 PM.


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