excel - How to use an If macro to delete columns if check box for that column isn't selected? -


i working spreadsheet 11 different system sizes on each system size representing column. need use able compare different system sizes need able select system sizes want @ times. example, system sizes 1300, 2000, 2000x, 2500, 2500x, 3000, 3000x, 4500, 6000, 7000, , 9000 , might need compare 2500, 3000, , 4500. have put check box in row 3 in each of these columns represent each system size , linked check box same cell in system 1300 check box in cell b3 , linked cell b3. want able go in , select each check box each system size want able run macro keep columns/system sizes check boxes selected , delete or hide columns/system sizes check boxes not selected. below code have system 1300.

dim system1300 string system1300 = range("b3").value if not system1300 "true" activesheet.shapes.range(array("check box 1")).select selection.delete columns("b:b").select range("b2").activate selection.delete shift:=xltoleft end if 

however, reason, whether have check box selected or not deletes check box , column. appreciated.

hiding seems more maintainable, that's did here:

sub hideuncheckedcolumns() dim ws excel.worksheet dim columncount long dim cell excel.range  set ws = activesheet 'adjust necessary columncount = 11 'adjust necessary ws     each cell in .range(.cells(3, 1), .cells(3, columncount))         cell.entirecolumn.hidden = cell.value = false     next cell end end sub 

edit: requested here's version deletes false (unchecked) columns.

the key logic difference have loop through cells backwards, i.e., right left. otherwise loop indexing break columns deleted:

sub deleteuncheckedcolumns() dim ws excel.worksheet dim columncount long dim long dim cell excel.range  set ws = activesheet    'adjust necessary columncount = 11    'adjust necessary ws     = columncount 1 step -1         set cell = .cells(3, i)         if cell.value = false             cell.entirecolumn.delete         end if     next end end sub 

you shorten eliminating cell variable , referring directly .cells(3, i). kept in partly comparison previous version, because think adds legibility code.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -

jquery - How can I dynamically add a browser tab? -