excel vba - Group rows by value -


how programmatically group following data values in column b?

note, there random values in columns a , c.

enter image description here

like this:

enter image description here --> enter image description here

try this

sub demo()     dim r range     dim v variant     dim long, j long      activesheet         on error resume next         ' expand groups on sheet         .outline.showlevels rowlevels:=8         ' remove existing groups         .rows.ungroup         on error goto 0         set r = .range("b1", .cells(.rows.count, 2).end(xlup))     end      r         'identify common groups in column b         j = 1         v = .cells(j, 1).value         = 2 .rows.count             if v <> .cells(i, 1)                 ' colum b changed, create group                 v = .cells(i, 1)                 if > j + 1                     .cells(j + 1, 1).resize(i - j - 1, 1).rows.group                 end if                 j =                 v = .cells(j, 1).value             end if         next         ' create last group         if > j + 1             .cells(j + 1, 1).resize(i - j - 1, 1).rows.group         end if         ' collapse groups         .parent.outline.showlevels rowlevels:=1     end end sub 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -