Help on single and subsequent ranges within groups with vba

pb0759

New Member
Joined
Apr 18, 2019
Messages
14
Hello,
I am trying to get records placed into two different columns depending on each grouping of ranges on dn1 and Ext in table example below:
If dn1/ext is asingle range then needs to be separated with a comma ',", else is in subsequent ranges then separation with a dash "-"
Results would need to be per each group like in Ext Results and dn1 Results column.
Any help would be greatly appreciated. :)
Groupdn1Ext Ext Resultsdn1 Results
Group A\+1972555100010001000,1031-1035,1055\+19725551000,\+19725551031-\+19725551035,\+19725551055
Group A\+197255510311031
Group A\+197255510321032
Group A\+197255510341034
Group A\+197255510351035
Group A\+197255510551055
Group B\+1972555103610361036-1037,1060\+19725551036-\+19725551037,\+19725551060
Group B\+197255510371037
Group B\+197255510601060

<tbody>
</tbody>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hello again..
I have noticed a problem with another scenario and wondering if you could please help out?
If the numbers are all in order the code is not working as should.
Example:

Results
Locdn1ExtExt Resultsdn1 Results
Group A\+1972555103110311031\+19725551031
Group A\+197255510321032
Group A\+197255510331033
Group A\+197255510341034
Group A\+197255510351035
Group B\+197255510361036
Group B\+197255510371037
Group B\+197255510381038
Group C\+1972555103910391039\+19725551039
Expected Results
Locdn1ExtExt Resultsdn1 Results
Group A\+1972555103110311031-1035\+19725551031-\+19725551035
Group A\+197255510321032
Group A\+197255510331033
Group A\+197255510341034
Group A\+197255510351035
Group B\+1972555103610361036-1038\+19725551036-\+19725551038
Group B\+197255510371037
Group B\+197255510381038
Group C\+1972555103910391039\+19725551039

<tbody>
</tbody>

Also if all are in Group A then I just get the last output number.
 
Upvote 0
Try this
Code:
Sub subsequent()
    Dim ini As Long, c As Range, ant As String, dn1 As String, ext As String, sep As String
    ini = 2
    Range("D:E").ClearContents
    ant = Range("A" & ini)
    exa = Range("C" & ini)
    For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp)(2))
        If ant <> c Then
            Cells(ini, "D").Value = "'" & Mid(ext, 2)
            Cells(ini, "E").Value = "'" & Mid(dn1, 2)
            dn1 = ""
            ext = ""
            ini = c.Row
        End If
        If c.Offset(, 2).Value - 1 = exa And ant = c Then sep = "-" Else sep = ","
        If c.Offset(, 2).Value + 1 <> c.Offset(1, 2).Value Or sep = "," Or c.Value <> c.Offset(1) Then
            dn1 = dn1 & sep & c.Offset(, 1)
            ext = ext & sep & c.Offset(, 2)
        End If
        ant = c
        exa = c.Offset(, 2)
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top