+ Reply to Thread
Results 1 to 6 of 6

VBA code to identify a continuous range

Hybrid View

rshnkmr39 VBA code to identify a... 11-07-2014, 08:21 AM
MickG Re: VBA code to identify a... 11-07-2014, 09:24 AM
rshnkmr39 Re: VBA code to identify a... 11-07-2014, 05:09 PM
MickG Re: VBA code to identify a... 11-08-2014, 06:11 AM
rshnkmr39 Re: VBA code to identify a... 11-09-2014, 12:08 AM
MickG Re: VBA code to identify a... 11-09-2014, 06:37 AM
  1. #1
    Registered User
    Join Date
    04-28-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    81

    VBA code to identify a continuous range

    Hi

    If I have a range

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    my macro should pick up the start range as 1 and end range as 15

    if the range is

    1 3 4 5 6 9 11 12 13 14 15

    my macro shoul pick up

    1 as a start 1 ,3 as start2 range and 5 as end2 , 9 as start3, 11 as start 4 and 15 as end15

    if the range is

    1 2 3 4 7 9 10 11 12 13 then

    1 is start1 4 is end1, 7 is start2, 9 is start3 13 is end3

    or even if my macro can high light the ones in color it is fine

    Basically identify a range and pick the start and end for each of the range and delete the inbetween numbers and retain the number which doesnt belong to the continuous series

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: VBA code to identify a continuous range

    Try this on numbers in row 1.
    NB:- See code comments to delete or Colour unwanted columns !!!
    Sub MG07Nov04
    Dim Lst As Long
    Dim c As Long
    Dim n As Long
    Dim Rng As Range
    Lst = Cells("1", Columns.Count).End(xlToLeft).Column
    For n = 1 To Lst
        If n = 1 Or n = Lst Then
            Cells(1, n).Interior.Color = vbGreen
        ElseIf Not Cells(1, n - 1) + 1 = Cells(1, n) Or Not Cells(1, n) + 1 = Cells(1, n + 1) Then
            Cells(1, n).Interior.Color = vbGreen
        Else
            If Rng Is Nothing Then
                Set Rng = Cells(1, n)
            Else
                Set Rng = Union(Rng, Cells(1, n))
            End If
        End If
    Next n
       'User the Line below to Colour unwanted columns
       'If Not Rng Is Nothing Then Rng.Interior.ColorIndex = 6
       
       'Use the line below to delete unwanted columns
       If Not Rng Is Nothing Then Rng.EntireColumn.Delete
    
    MsgBox "Run"
    End Sub
    Regards Mick

  3. #3
    Registered User
    Join Date
    04-28-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: VBA code to identify a continuous range

    Hi this code removes the unwanted set of numbers..But needs to return the result in sets
    like

    1 to 4 is consequitive numbers so remove 2 n 3
    9 to 13 is consequitive so remove 10,11 and 12
    1-4,7,9-13
    Last edited by rshnkmr39; 11-07-2014 at 05:14 PM.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: VBA code to identify a continuous range

    On the basis you want each set in a different cell, try this.
    Results start "A5"
    Sub MG08Nov37
    Dim Lst As Long, n As Long
    Dim Str As String, Sp As Variant
    Dim nSp As Variant
    Lst = Cells("1", Columns.Count).End(xlToLeft).Column
    
    For n = 1 To Lst
       If n = 1 Then
            Str = Cells(1, n)
       ElseIf Cells(1, n) = Cells(1, n - 1) + 1 Then
            Str = Str & "#" & Cells(1, n)
       Else
             Str = Str & "," & Cells(1, n)
        End If
    Next n
    Sp = Split(Str, ",")
        For n = 0 To UBound(Sp)
            If InStr(Sp(n), "#") Then
                nSp = Split(Sp(n), "#")
                Sp(n) = nSp(0) & "-" & nSp(UBound(nSp))
            End If
     Next n
    With Range("A5")
        .EntireRow.Clear
        .Resize(, UBound(Sp) + 1) = Sp
    End With
    End Sub
    Regards Mick
    Last edited by MickG; 11-08-2014 at 06:59 AM.

  5. #5
    Registered User
    Join Date
    04-28-2013
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    81

    Re: VBA code to identify a continuous range

    WOw.This is amazing.Thanks a ton.

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: VBA code to identify a continuous range

    You're welcome

+ 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: 0
    Last Post: 02-20-2012, 05:42 PM
  2. Continuous Range
    By DSC174 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-10-2005, 01:05 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