vba - How to transfer highlighted cells in Excel 2007 from one table to another in the same sheet? -
i create code transfer content of highlighted cells 1 table in same sheet content, use button copy content, create macro transfer content dynamically clicking on button, when user change content of highlighted cells of first table content changes automatically in second table or clicking on button again.
i use code highlight cells
' set of highlighted cells indexed row number dim highlightedcells new collection ' scan existing sheet cells coloured 'red' , initialise ' run-time collection of 'highlighted' cells. private sub worksheet_activate() activesheet.unprotect password:="p@ssw0rd" dim existinghighlights range ' reset collection of highlighted cells ready rebuild set highlightedcells = new collection ' find first cell has background coloured red application.findformat.interior.colorindex = 3 set existinghighlights = activesheet.cells.find("", _ lookin:=xlvalues, _ lookat:=xlpart, _ searchorder:=xlbyrows, _ searchdirection:=xlnext, _ matchcase:=false, _ searchformat:=true) ' process long have more matches while not existinghighlights nothing crow = existinghighlights.row ' add reference first coloured cell if multiple ' exist in single row (will occur if background manually set) err.clear on error resume next call highlightedcells.add(existinghighlights.address, cstr(crow)) on error goto 0 ' search cell after last match. note error in excel ' appears prevent findnext method finding formats correctly application.findformat.interior.colorindex = 3 set existinghighlights = activesheet.cells.find("", _ after:=existinghighlights, _ lookin:=xlvalues, _ lookat:=xlpart, _ searchorder:=xlbyrows, _ searchdirection:=xlnext, _ matchcase:=false, _ searchformat:=true) ' abort search if we've looped top of sheet if (existinghighlights.row < crow) exit end if loop activesheet.protect password:="p@ssw0rd" end sub private sub worksheet_beforedoubleclick(byval target range, cancel boolean) activesheet.unprotect password:="p@ssw0rd" dim hcell string dim cellalreadyhighlighted boolean hcell = "" err.clear on error resume next hcell = highlightedcells.item(cstr(target.row)) on error goto 0 if (hcell <> "") activesheet.range(hcell).interior.colorindex = 2 if (hcell = target.address) call highlightedcells.remove(cstr(target.row)) target.interior.colorindex = 2 else call highlightedcells.remove(cstr(target.row)) call highlightedcells.add(target.address, cstr(target.row)) target.interior.colorindex = 3 end if else err.clear on error resume next highlightedcells.remove (cstr(target.row)) on error goto 0 call highlightedcells.add(target.address, cstr(target.row)) target.interior.colorindex = 3 end if cancel = true activesheet.protect password:="p@ssw0rd" end sub
and use code copy highlighted cells:
sub copycat() activesheet.unprotect password:="p@ssw0rd" dim lr long, long, j long dim c range j = 1 lr = range("a" & rows.count).end(xlup).row each c in worksheets("mb").range("a1:o" & lr) if c.interior.colorindex = 3 c.copy destination:=worksheets("mb").range("j" & j) j = j + 1 end if next c activesheet.protect password:="p@ssw0rd" end sub
please !!!!
instead of copying whole table , using values populate table on second page, why not (for items want update, sheet1 gets updates) leave "link" original table. either set literally cell refers to, or more robustly, use index/match. see below:
this example of sheet1 (the data want copied onto second sheet). have highlighted "salary" column, reflect user asked change these.
and in sheet 2, can use various ways of "linking" first sheet:
that way, when go in edit salary chris or john, it'll update salary in second sheet, without needing run macros. you're looking do, or overlooking/misunderstanding something?
Comments
Post a Comment