excel - copy cell background color and past it corresponding cell of another sheet -
i have been trying write macro copy cell background color , past corresponding cell of sheet.i have lots of values on sheet 1 , gave background color using conditional formatting ,after want copy color , past corresponding cell of sheet 2 without pasting value.example if sheet 1 cell a1 has red color specific value ,i want transfer color sheet 2 a1. giving picture of excel sheet 1 , code.
sub copycolor() dim introw integer dim rngcopy range dim rngpaste range introw = 1 20 set rngcopy = sheet1.range("a" & introw + 0) set rngpaste = sheet2.range("b" & introw) 'test see if rows 500+ have value if rngcopy.value <> "" 'since has value, copy value , color rngpaste.value = rngcopy.value rngpaste.interior.color = rngcopy.interior.color end if next introw end sub
i use conditional formatting giving color , here use 2 color.one red , 1 white.red use higher value , white lower vaue. nice if me out problem.
rngpaste.interior.color = rngcopy.displayformat.interior.color
seems work me. keep in mind displayformat read-only , not allowed return value outside of function it's used in. available in excel 2010 +
i editing answer include other stuff mentioned , realized getting confusing explain in separate chunks. here's recommended approach achieve you're saying.
public sub copycolor() dim sourcesht worksheet dim targetsht worksheet dim rngcopy range dim rngpaste range dim lastcopyrow long dim lastcopycolumn long 'define our source sheet , target sheet set sourcesht = thisworkbook.worksheets("sheet1") set targetsht = thisworkbook.worksheets("sheet2") 'find our used space on source sheet lastcopyrow = sourcesht.cells(rows.count, "a").end(xlup).row lastcopycolumn = sourcesht.cells(1, columns.count).end(xltoleft).column 'setup our ranges can sure don't loop through unused space set rngcopy = sourcesht.range("a1:" & sourcesht.cells(lastcopyrow, lastcopycolumn).address) set rngpaste = targetsht.range("a1:" & targetsht.cells(lastcopyrow, lastcopycolumn).address) 'loop through each row of each column. ' go through each cell in column 1, move on column 2 col = 1 lastcopycolumn cel = 1 lastcopyrow ' if string value of our current cell not empty. if rngcopy.cells(cel, col).value <> "" 'copy source cell displayed color , paste in target cell rngpaste.cells(cel, col).interior.color = rngcopy.cells(cel, col).displayformat.interior.color end if next cel next col end sub
Comments
Post a Comment