vba - Excel userform deleting entire row not just intended selction -
i have userform has drop down box in person can select record have deleted off list.
the code below deleting entire row. not want that. want cells between a:e cleared on spreadsheet.
i not sure how else describe apologize in advance. here code:
private sub checkbox1_click() end sub private sub commandbutton1_click() dim lrw long activeworkbook.sheets("rawdata").visible = xlsheetvisible 'get row number. add 2 because listindex starts @ 1 lrw = me.combobox1.listindex + 2 activeworkbook.sheets("rawdata").select cells(lrw, 1).entirerow.clearcontents activeworkbook.sheets("rawdata").visible = xlsheethidden end sub private sub commandbutton2_click() combobox1.value = "" combobox1.clear combobox1.clear unload me end sub private sub userform_initialize() 'assumes data starts in a1 , has header row me.combobox1.list = activeworkbook.sheets("rawdata").cells(1, 2).currentregion.offset(1, 2).value end sub
the cells(lrw, 1).entirerow.clearcontents
issue. entirerow
function selects row pointed cells(lrw, 1)
. .clearcontents
function clears what's selected. should replace like:
range("a" & <the row number> & ":j" & <the row number>).clearcontents
your variable lrw
supposed hold value of row in selected project located, correct? if so, then:
range("a" & lrw & ":j" & lrw ).clearcontents
should work. can change column letters whatever you'd clear.
i think pj rosenburg's solutions bit impractical, agree fact should shy away using .select
function. can need without using it. you'll write better code once understand concept. in fact, here's rewrite of commandbutton1_click should exact same thing, less code , easier read.
private sub commandbutton1_click() dim lrw long lrw = me.combobox1.listindex + 2 activeworkbook.sheets("rawdata") .visible = xlsheetvisible .range("a" & lrw & ":j" & lrw ).clearcontents .visible = xlsheethidden end end sub
notice couple of things:
- no
.select
- moving assignment statement
- the addition of with/end statements
anyway, hope helps , better explains trying earlier.
Comments
Post a Comment