excel - Merge Cells with Duplicate Data VBA -
i'm trying macro working merge cells duplicate data. work on small numbers of cells, following error if try run on larger group of cells. i'm not sure if there's more efficient way excel run through this.
run-time error '1004': method 'range' of object '_global' failed
here's code:
sub mergeduplicates() dim vardata variant, varcontent variant dim strmyrange string application.screenupdating = false application.displayalerts = false strmyrange = activecell.address varcontent = activecell.value each vardata in selection.cells if vardata.value <> varcontent strmyrange = strmyrange & ":" & cells(vardata.row - 1, vardata.column).address & ", " & cells(vardata.row, vardata.column).address varcontent = cells(vardata.row, vardata.column).value end if next strmyrange = strmyrange & mid(selection.address, instr(1, selection.address, ":"), len(selection.address)) range(strmyrange).merge application.displayalerts = true application.screenupdating = true end sub
i have recreated issue using code posted , working me. did suggested , put merge loop. split strmyrange using comma delimiter. set test ":" character in testarray(0). if in target string, know ready merge. after reset strmyrange testarray(1) beginning of next range.
note: able step through debugger 100 cells , worked. tried running without code breakpoints, merged selected cells. put 1 second wait statement right before final merge , seems work.
here code:
sub mergeduplicates() dim vardata variant, varcontent variant dim strmyrange string dim testarray() string dim target string dim pos integer application.screenupdating = false application.displayalerts = false strmyrange = activecell.address varcontent = activecell.value each vardata in selection.cells if vardata.value <> varcontent strmyrange = strmyrange & ":" & cells(vardata.row - 1, vardata.column).address & ", " & cells(vardata.row, vardata.column).address testarray = split(strmyrange, ",") target = testarray(0) pos = instr(target, ":") if (pos > 0) range(target).merge strmyrange = testarray(1) end if varcontent = cells(vardata.row, vardata.column).value end if next strmyrange = strmyrange & mid(selection.address, instr(1, selection.address, ":"), len(selection.address)) application.wait (now + #12:00:01 am#) 'this helps application run ok if there no breakpoints. range(strmyrange).merge application.displayalerts = true application.screenupdating = true end sub
Comments
Post a Comment