+ Reply to Thread
Results 1 to 1 of 1

Breaking up multiple numeric ranges - questions?

Hybrid View

Rhudi Breaking up multiple numeric... 02-19-2014, 10:28 AM
  1. #1
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    201

    Breaking up multiple numeric ranges - questions?

    I am working with a list of vlan ranges. Some of these will be ranges: "101-120" or individual numbers: "150,158,201" or even a combination: "101-120,150,158,201,300-450,2000-2500".
    What I needed to do is break this up into an array that contains every number that is part of the whole listed group. That array is used to compare if a number from something else is a match; part of the listed range.
    In the numbers above, 105, 150, 302,2000,2499 or 2500 would match. But, 121, 155, 210, 1999, 2501 would not match.

    The source data comes from a Cisco switch configuration file, obtained with 'show run'. Within that text file will be an area similar to this:
    !
    interface GigabitEthernet5/1
     switchport
     switchport trunk encapsulation dot1q
     switchport trunk allowed vlan 1-1316,1318-1334,1336-3371,3376-3415,3420-3423
     switchport trunk allowed vlan add 3428-3465,3468-3483,3500-3539,3576-3587
     switchport trunk allowed vlan add 3600-4094
     switchport mode trunk
     mtu 9216
    !
    I open the text file into an array, where each element of the array is loaded with the text file with the appropriate vbLf (or vbCrLf - I have a way to be sure).
    When the array is loaded (has more than -1 elements) I can use Application.Match to find the line that has the interface line I'm using.
    I will use:
        t = Application.Match("interface GigabitEthernet" & Mid(Sheets(1).Range("B4"), 3), sDataArray, 0)
        If Not IsError(t) Then ...
    If 't' is an error, it wasn't found and I can go do stuff. If the string is found, it will be a number that is an index within the array to the next line.
    In this example, sDataArray(t) will be " switchport". I will use a do-loop until I find the trailing "!".
    During that loop, I will add the numeric parts to an intermediate string until I have every line.
    The code for that, looks like this:
        t = Application.Match("interface GigabitEthernet" & Mid(Sheets(1).Range("B4"), 3), sDataArray, 0)
        If Not IsError(t) Then
            VLANList = ""
            Do Until sDataArray(t) = "!"
                If Left(sDataArray(t), 31) = " switchport trunk allowed vlan " Then
                    If Left(Mid(sDataArray(t), 31), 5) = " add " Then
                        VLANList = VLANList & Mid(sDataArray(t), 36) & ","
                    Else
                        VLANList = VLANList & Mid(sDataArray(t), 32) & ","
                    End If
                End If
                t = t + 1
            Loop
            If Right(VLANList, 1) = "," Then VLANList = Left(VLANList, Len(VLANList) - 1)
        Else
            MsgBox "The switch ( " & Range("D4") & " ) does not appear to use Port " & "GigabitEthernet" & Mid(Sheets(1).Range("B4"), 3)
            End
        End If
    The variable VLANList will wind up with this (from the above example):
    "1-1316,1318-1334,1336-3371,3376-3415,3420-3423,3428-3465,3468-3483,3500-3539,3576-3587,3600-4094"

    When that variable is set, I do this:
        sVLANArray = Split(VLANList, ",")
    
        VLANList = ""
        If UBound(sVLANArray) = -1 Then
            MsgBox "No VLANs are specified." & vbLf & "Assume 1 through 4094 are used!"
            For z = 1 To 4094
                VLANList = VLANList & z & ","
            Next z
            sVLANArray = Split(VLANList, ",")
        End If
        For y = LBound(sVLANArray) To UBound(sVLANArray)
            iVLANArray = Split(sVLANArray(y), "-")
            For z = iVLANArray(LBound(iVLANArray)) To iVLANArray(UBound(iVLANArray))
                VLANList = VLANList & z & ","
            Next z
        Next y
    
        VLANArray = Split(VLANList, ",")
        VLANList = ""
    When this is done, VLANArray will contain all the individual numbers that are within the skipping lists of numbers. I realise there is a potential glitch here, if the VLANList has no 'ranges', but is instead only a 'skip-list' (I believe I figured that out).

    I then use Application.Match to find the values I need to confirm.

    I have two questions: 1) is this horribly clunky? 2) Can I confirm that only exact matches will be found? I don't want to be looking for 310 and get a false match because the list has something like 3101.
    Last edited by Rhudi; 02-25-2014 at 10:56 AM. Reason: Correction to post

+ 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. Replies: 6
    Last Post: 07-04-2012, 09:31 AM
  2. Breaking a number into ranges of values in Excel 2010
    By skennedyaz in forum Excel General
    Replies: 5
    Last Post: 04-20-2012, 02:45 AM
  3. Macro - Breaking Number Ranges
    By rfarrell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-01-2008, 02:27 PM
  4. Match Single Numeric Criteria and Return Multiple Numeric Labels
    By Sam via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-30-2005, 04:10 PM
  5. Match Single Numeric Criteria and Return Multiple Numeric Labels
    By Sam via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-29-2005, 04:50 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