I have been searching near and far for vba code that can let me do this. I found someones code but I couldn't get it to work
Anyone come across something like this before?
Below is the code I came across:
Much thanks in advance.
Below is the code I came across:
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">[COLOR=#00008B]Public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR][COLOR=#000000] GroupCells[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] myRange [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Range
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] rowCount [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] currentRow [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] firstBlankRow [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] lastBlankRow [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] currentRowValue [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] neighborColumnValue [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'select range based on given named range[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Set[/COLOR][COLOR=#000000] myRange [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Range[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"rngList"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
rowCount [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]Rows[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Count[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] myRange[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Column[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]xlUp[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Row
firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
lastBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'for every row in the range[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#000000] currentRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]To[/COLOR][COLOR=#000000] rowCount
currentRowValue [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentRow[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] myRange[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Column[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
neighborColumnValue [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentRow[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] myRange[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Column [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]IsEmpty[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentRowValue[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Or[/COLOR][COLOR=#000000] currentRowValue [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]""[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'if cell is blank and firstBlankRow hasn't been assigned yet[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000] firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] currentRow
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]ElseIf[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Not[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]IsEmpty[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentRowValue[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Or[/COLOR][COLOR=#000000] currentRowValue [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]""[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'if the cell is not blank and its neighbor's (to the left) value is 0,[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'and firstBlankRow hasn't been assigned, then this is the firstBlankRow[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'to consider for grouping[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000] neighborColumnValue [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]And[/COLOR][COLOR=#000000] firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] currentRow
[/COLOR][COLOR=#00008B]ElseIf[/COLOR][COLOR=#000000] neighborColumnValue [/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]And[/COLOR][COLOR=#000000] firstBlankRow [/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'if firstBlankRow is assigned and this row has a value with a neighbor[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'who isn't 0, then the cell one row above this one is to be considered[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'the lastBlankRow to include in the grouping[/COLOR][COLOR=#000000]
lastBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] currentRow [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'if first AND last blank rows have been assigned, then create a group[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'then reset the first/lastBlankRow values to 0 and begin searching for next[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]'grouping[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000] firstBlankRow [/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]And[/COLOR][COLOR=#000000] lastBlankRow [/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
Range[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]firstBlankRow[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] myRange[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Column[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#000000] Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]lastBlankRow[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] myRange[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Column[/COLOR][COLOR=#000000])).[/COLOR][COLOR=#000000]EntireRow[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Select[/COLOR][COLOR=#000000]
Selection[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Group
firstBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
lastBlankRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]Next[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#00008B]Sub[/COLOR]</code>
Much thanks in advance.