excel vba - Store whole format of a cell in a variable -
background
i want write simple function swaps contents of 2 selected (not adjacent) cells. do not want copy cell temporary cell first. thus, want swap cells in place.
challenge
while swaping content rather easy using variant
temporary variable holding content of cell 1, overwriting content of cell 1 content of cell 2 , writing content of variant variable cell 2, struggle how copy format related stuff. there plenty of slots need consideration (.numberformat, .interior
name two). need copy each of them seperately or there easier way swap format without using temporary cell?
code
public sub swapcells(optional bolwithformat boolean = true) 'purpose: switch content of 2 cells on error goto errhandler dim rngsel range dim varcontent variant set rngsel = selection if (rngsel.count = 2) rngsel.cells(1) varcontent = .value .value = rngsel.cells(2).value rngsel.cells(2).value = varcontent end else 'do nothing, because swap makes sense 2 cells end if errhandler: set rngsel = nothing end sub
per comments, using temporary holding cell easiest solution. can use cell in personal macro workbook avoid worrying finding spare cell in active workbook. wise set saved
property of personal workbook true afterwards avoid getting prompted save every time quit excel after running macro!
Comments
Post a Comment