Excel VBA - Finding the beginning and end of coloured rows -


i trying create code in excel vba, locate beginning (cell address) , end (cell address) of coloured rows in table. table timeline(horizontal axis- dates, vertical axis - general text). coloured rows not start in first column, start in different columns. help?

how's this?

sub findcoloredrows() dim startcol integer, endcol integer, o integer dim ws            worksheet dim integer, k integer dim startrow long, endrow long dim cellcolor string, nocolor string dim cel           range  nocolor = -4142              ' color index of no coloring  k = 3  set ws = activesheet ws     startrow = .cells(1, 3).end(xldown).row     startcol = .cells(1, 3).column     while startrow > 100  ' assume table starts before row 100.  so, if there's no data before row 100, check next column         k = k + 1         startrow = .cells(1, k).end(xldown).row         startcol = k     loop      'now, have our starting row - end row.     endrow = .cells(startrow, k).end(xldown).row      endcol = .cells(startrow, startcol).end(xltoright).column     debug.print "start row: " & startrow & ", start column: " & startcol      ' how many non colored cells there in our range?     dim nocolorcells integer     each cel in .range(.cells(startrow, startcol), .cells(endrow, endcol))         if cel.interior.colorindex = nocolor             nocolorcells = nocolorcells + 1         end if     next cel     debug.print "there " & nocolorcells & " non colored cells."      .cells(startrow - 1, endcol + 2).value = "start date"     .cells(startrow - 1, endcol + 3).value = "end date"      'redim array fit colored cells     redim tdates(1 nocolorcells + 1)     = 1                    'index starts @ 1, set 1      k = startrow endrow         o = startcol endcol             if .cells(k, o).interior.colorindex = nocolor , .cells(k, endcol + 2) = ""                 .cells(k, endcol + 2).value = .cells(k, o).value             elseif .cells(k, o).interior.colorindex = nocolor , .cells(k, endcol + 2)                 = +                 .cells(k, endcol + 3).value = .cells(k, o).value             end if             ' = + 1         next o          = + 1     next k  end  msgbox ("done!")  end sub 

this sub find addresses of colored cells. if can explain more mean "locate beginning , end of coloured rows in table." can tweak this. can post image of sample table maybe?

edit: per discussion below, try in case there's not data in table, want columns of colored cells:

sub findcoloredbgcells() dim startrow integer, endrow integer, integer, k integer, startcol integer, endcol integer dim cellcolor string, nocolor string dim ws worksheet  set ws = activesheet nocolor = -4142  ws     'get starting row     startrow = .cells(1, 1).end(xldown).row     endrow = .cells(startrow, 1).end(xldown).row      ' since know names start , end (less 1 "names" part), let's count how many names have     dim nonames integer     nonames = endrow - startrow     if not isempty(.cells(1, 1)) ' first used column data         startcol = 1     elseif isempty(.cells(1, 1))         startcol = .cells(1, 1).end(xltoright).column     end if      endcol = .cells(1, startcol).end(xltoright).column      'now have our range, let's use loop blank cells, , add array     dim coloredcells() variant     redim coloredcells(1 nonames, 2)     dim rng range, cel range     set rng = .range(.cells(startrow, startcol), .cells(endrow, endcol))     'rng.select      'now, count how many cells not blank background     dim cnt integer, celrow integer, lastcelrow integer     = 1      lastcelrow = 2     each cel in rng         cel.select         celrow = cel.row         if cel.row <> lastcelrow 'this can change first dimension in array             k = k + 1             coloredcells(k, 0) = .cells(cel.row, 1).value             = 1            ' = + 1         end if          if cel.interior.colorindex <> nocolor             cnt = cnt + 1             if > 2 = 2 'since it's 2 dimensions need, go '1'            ' redim preserve coloredcells(nonames, i) 'resize array hold new column             coloredcells(k, i) = .cells(1, cel.column).value             = + 1         end if         lastcelrow = celrow     next cel  k = 1 ubound(coloredcells)         debug.print coloredcells(k, 0) & " start date: " & coloredcells(k, 1) & ", end date: " & coloredcells(k, 2) & "."         .cells(2 + k, 2).value = coloredcells(k, 1)         .cells(2 + k, 3).value = coloredcells(k, 2) next k  end  msgbox ("done!")  end sub 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -