excel - Function to delete columns needs to be run 6 times to work as expected -


i have written following macro.

sub seasonteas() dim lastcolumn long lastcolumn = sheet1.cells(1, columns.count).end(xltoleft).column dim arrsize long arraysize = lastcolumn - 1 dim elem long redim selectcolumns(arraysize) x = 1 (lastcolumn)     if cells(1, x).value <> "respid" , cells(1, x).value <> "status" , cells(1, x).value <> "cid"         columns(x).select         selection.delete shift:=xltoleft     end if next x end sub 

i need find columns not match respid/status/cid , delete else.

it needs run 6 times need.

i know there more efficient way working way before trying else.

the reason need run macro 6 times desired result delete column, remaining columns move left , column right of deleted column not tested on next loop iteration.

for example, column 1 deleted column 2 becomes column 1 on next loop, x=2 new column 1 doesn't tested.

sub seasonteas2() dim lastcolumn long dim x long  'define last column lastcolumn = sheet1.cells(1, columns.count).end(xltoleft).column  'step backwards  last column x = lastcolumn 1 step -1     if cells(1, x).value <> "respid" , cells(1, x).value <> "status" , cells(1, x).value <> "cid"         columns(x).delete shift:=xltoleft     end if next x end sub 

stepping through code using f8 allows see each command executed can see if code behaving expected.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -