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. :)
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Group[/TD]
[TD]dn1[/TD]
[TD]Ext [/TD]
[TD]Ext Results[/TD]
[TD]dn1 Results[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551000[/TD]
[TD]1000[/TD]
[TD]1000,1031-1035,1055[/TD]
[TD]\+19725551000,\+19725551031-\+19725551035,\+19725551055[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551031[/TD]
[TD]1031[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551032[/TD]
[TD]1032[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551034[/TD]
[TD]1034[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551035[/TD]
[TD]1035[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551055[/TD]
[TD]1055[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551036[/TD]
[TD]1036[/TD]
[TD]1036-1037,1060[/TD]
[TD]\+19725551036-\+19725551037,\+19725551060[/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551037[/TD]
[TD]1037[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551060[/TD]
[TD]1060[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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:

[TABLE="width: 577"]
<tbody>[TR]
[TD]Results[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Loc[/TD]
[TD]dn1[/TD]
[TD]Ext[/TD]
[TD]Ext Results[/TD]
[TD]dn1 Results[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551031[/TD]
[TD]1031[/TD]
[TD="align: right"]1031[/TD]
[TD]\+19725551031[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551032[/TD]
[TD]1032[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551033[/TD]
[TD]1033[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551034[/TD]
[TD]1034[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551035[/TD]
[TD]1035[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551036[/TD]
[TD]1036[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551037[/TD]
[TD]1037[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551038[/TD]
[TD]1038[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD]\+19725551039[/TD]
[TD]1039[/TD]
[TD]1039[/TD]
[TD]\+19725551039[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Expected Results[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Loc[/TD]
[TD]dn1[/TD]
[TD]Ext[/TD]
[TD]Ext Results[/TD]
[TD]dn1 Results[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551031[/TD]
[TD]1031[/TD]
[TD]1031-1035[/TD]
[TD]\+19725551031-\+19725551035[/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551032[/TD]
[TD]1032[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551033[/TD]
[TD]1033[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551034[/TD]
[TD]1034[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group A[/TD]
[TD]\+19725551035[/TD]
[TD]1035[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551036[/TD]
[TD]1036[/TD]
[TD]1036-1038[/TD]
[TD]\+19725551036-\+19725551038[/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551037[/TD]
[TD]1037[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group B[/TD]
[TD]\+19725551038[/TD]
[TD]1038[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Group C[/TD]
[TD]\+19725551039[/TD]
[TD]1039[/TD]
[TD="align: right"]1039[/TD]
[TD]\+19725551039[/TD]
[/TR]
</tbody>[/TABLE]

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,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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