excel - Unmerge merged cell when empty -
i'm trying code right fail @ point: have excel sheet merges cell 1 underneath depending on value that's written in top cell, when value deleted merged cell i'd cells unmerge. try work sub keep code bit clean (don't know proper rules try) :
public sub letitmerge(target object) if target.value = empty call unmergecell(target) else if (target.value = vv call mergecell(target) end if end sub sub unmergecell(m object) m.resize(1, 1).unmerge m.borders(xlinsidehorizontal).linestyle = xllinestyle.xlcontinuous end sub sub mergecell(n object) n.resize(2).merge 'merge cells n.verticalalignment = xlcenter 'center text n.horizontalalignment = xlcenter 'center text end sub
a few minor tweaks code, appears working me.
the worksheet_change
event fires time there change on worksheet. based on value of changed cell, run either of unmerge
or mergecell
subroutines.
public sub worksheet_change(byval target range) 'use target.cells(1) since merged cell.' if target.cells(1).value = vbnullstring call unmergecell(target) else if target.value = vv call mergecell(target) end if end sub sub unmergecell(m range) m.resize(1, 1).unmerge m.borders(xlinsidehorizontal).linestyle = xllinestyle.xlcontinuous end sub sub mergecell(n range) n.resize(2).merge 'merge cells n.verticalalignment = xlcenter 'center text n.horizontalalignment = xlcenter 'center text end sub
update #1
if have multiple worksheets, can put above code in standard code module, , rename sub worksheet_change(...
else sub letitmerge(target range)
then, in each worksheet's code module, still need change
event macro each worksheet, this:
sub worksheet_change(byval target range) letitmerge target end sub
so now, _change
event on each worksheet trigger letitmerge
routine, determines of unmerge
or mergecells
subroutines execute.
Comments
Post a Comment